Two relatively straight forward tasks this week, so not as much commentary as usual.
You are given an integer,
$n > 0.
Write a script to find all possible binary numbers of size
Thankfully both Python and Perl provide easy methods to convert a (base 10) integer into a binary number with leading zeros. For the integer
y leading zeros, we can use
format(x, '0yb') in Python, and
sprintf('%0yb', x) in Perl.
With that out of the way, we know that all binary numbers can be converted from the integers between 0 and 2n-1 (inclusive).
$ ./ch-1.py 2 00, 01, 10, 11 $ ./ch-1.py 3 000, 001, 010, 011, 100, 101, 110, 111
You are given a list of strings of same length,
Write a script to find the odd string in the given list. Use positional value of alphabet starting with
a = 0, b = 1, ... z = 25.
Find the difference array for each string as shown in the example. Then pick the odd one out.
This task can be broken into three sub tasks:
- Create a dict (hash in Perl) called
letter_mapthat maps the letters of the alphabet to integers, where
letter_map['a']is 0, and
- Convert each word into a space separated values of differences. So
adcis converted to
3 -1. With this information I have an
occurrencesdict where the key is the converted string and the value is a list of words that match that pattern.
- Calculate an array
unique_wordsof values in
occurrencesthat only have one value. If there is only one unique word, I display that. If not, I display an error.
$ ./ch-2.py adc wzy abc abc $ ./ch-2.py aaa bob ccc ddd bob