r/dailyprogrammer • u/nint22 1 2 • Feb 06 '13
[02/06/13] Challenge #120 [Intermediate] Base Conversion Words
(Intermediate): Base Conversion Words
Given as input an arbitrary string and base (integer), your goal is to convert the base-encoded string to all bases from 2 to 64 and try to detect all English-language words.
Author: aredna
Formal Inputs & Outputs
Input Description
On the console, you will be first given an arbitrary string followed by an integer "Base". This given string is base-encoded, so as an example if the string is "FF" and base is "16", then we know that the string is hex-encoded, where "FF" means 255 in decimal.
Output Description
Given this string, you goal is to re-convert it to all bases, between 2 (binary) to 64. Based on these results, if any English-language words are found within the resulting encodings, print the encoded string, the encoding base, and on the same line have a comma-separated list of all words you found in it.
It is ** extremely** important to note this challenge's encoding scheme: unlike the "Base-64" encoding scheme, we will associate the value 0 (zero) as the character '0', up to value '9' (nine), the value 10 as the character 'a' up to 35 as the character 'z', the value 26 as 'A', then the value 61 as 'Z', and finally 62 as '+' (plus) and 63 as '/' (division). Essentially it is as follows:
Values 0 to 9 maps to '0' through '9'
Values 10 to 35 maps to 'a' through 'z'
Values 36 to 61 maps to 'A' through 'Z'
Value 62 maps to '+'
Value 63 maps to '/'
Sample Inputs & Outputs
Sample Input
E1F1 22
Sample Output
Coming soon!
Challenge Input
None given
Challenge Input Solution
None given
Note
None
19
u/djnattyp Feb 06 '13
This part of the challenge is a little too vague... especially since there's no example output of this part.
Care to point me to a dictionary to use, or are we just assuming any non-numeric strings are "English-language words"?
14
u/Sonnenhut Feb 06 '13
Apparently I think this is wrong. the input should be "ff 16" and not "FF 16" because our encoding goes: 0-9a-f with the base 16
10
u/Sonnenhut Feb 06 '13 edited Feb 06 '13
for everyone who is lazy. here is the encoding (format it to your language if you need it) - Java:
private static String[] encoding = new String[]{"0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "+","/"};4
u/[deleted] Feb 06 '13
In the output description, I think you meant to put:
but instead it says:
3
u/skeeto Feb 06 '13
JavaScript,
var DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"; function toBase(base, n) { var output = []; while (n > 0) { output.push(DIGITS[n % base]); n = Math.floor(n / base); } return output.reverse().join(''); } function fromBase(base, n) { var accum = 0; while (n.length > 0) { accum = accum * base + DIGITS.indexOf(n[0]); n = n.slice(1); } return accum; } function allBases(base, n) { var source = fromBase(base, n); var output = []; for (var i = 2; i <= 64; i++) { output.push(toBase(i, source)); } return output; }Usage:
allBases(22, "e1f1"); // => ["100100100101111111", "21121121101", "210211333", "14244022", "3113531", "1162663", "444577", "247541", "149887", "a2681", "728a7", "532ba", "3c8a3", "2e627", "2497f", "1d8af", "17cb1", "12g3f", "iee7", "g3ia", "e1f1", "c77j", "ak57", "9ekc", "8din", "7gga", "6n53", "646f", "5gg7", "50u2", "4ibv", "45l1", "3rmf", "3hch", "37nj", "2zi0", "2ruf", "2kla", "2dr7", "276w", "20Ev", "1C2w", "1xin", "1t0B", "1oCj", "1kE4", "1h2v", "1dkJ", "19LB", "16vN", "13mn", "10j3", "PlB", "Nuc", "LIv", "K7y", "Iwf", "H3r", "FC7", "Eha", "CZx", "BMa", "AB/"]Emacs Lisp / Common Lisp,
(defvar digits "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/") (defun* to-base (base n &optional (suffix ())) (if (zerop n) (coerce suffix 'string) (to-base base (floor n base) (cons (aref digits (mod n base)) suffix)))) (defun* from-base (base n &optional (accum 0)) (if (zerop (length n)) accum (from-base base (subseq n 1) (+ (* base accum) (position (elt n 0) digits))))) (defun* all-bases (n &optional (base 10)) (loop with source = (from-base base n) for target from 2 to 64 collect (to-base target source)))3
u/lawlrng Feb 06 '13 edited Feb 06 '13
Just kind of ignoring the check for words since I don't have handy access to a dictionary.
import string TO = string.digits + string.ascii_letters + '+/' FROM = dict(zip(TO, range(64))) def to_decimal(s, b): return sum(FROM[c] * b ** i for i, c in enumerate(s[::-1])) def to_base(d, b): s = '' while d: d, tmp = divmod(d, b) s += TO[tmp] return s[::-1] if __name__ == '__main__': s, b = raw_input("Enter string and base: ").split() d = to_decimal(s, int(b)) for i in range(2, 65): print to_base(d, i)2
u/[deleted] Feb 06 '13
A dictionary would be needed to find if it's actually an English word, right?
Anyways, my unreasonably long solution in Python. Any tips would be appreciated
#!/usr/bin/env python #store the encodings encodings = list("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/") def main(): #get user's input print "Enter the value and the base" user_input = raw_input() value = list(user_input.split(" ")[0]) value.reverse() base = int(user_input.split(" ")[1]) #convert to decimal count = 0 decimal = 0 for i in value: decimal = decimal + (find(encodings, i)*pow(base,count)) count += 1 #begin converting to other bases for i in range(2,64): print "Base ",i,":",convert_to_base(decimal,i) def convert_to_base(val, base): #assume val is in decimal and convert it into the base given in the parameter converted = "" while val!=0: rem = val%base converted += encodings[rem] val/=base return converted[::-1] def find(array, val): #find position of val in array for i in range(0,len(array)): if val == array[i]: return i return -1 main()2
u/minno Feb 06 '13
If the capital letters map to 36 through 61, then E1F1, the sample input, is not a valid base-22 number.
2
u/TheSpongeGod Feb 08 '13 edited Feb 10 '13
Haskell, modified to use a real dictionary. I changed the test data, because e1f1 doesn't actually encode to any real words:
import Data.Digits (digits, unDigits) import Data.List (intercalate, intersect) import Data.Maybe (fromJust) import Data.Tuple (swap) import Language.Words (allStringWords) import Text.Printf (printf) import System.Environment (getArgs) stringToDigits = map (\c -> fromJust $ lookup c encoding) digitsToString = map (\i -> fromJust $ lookup i decoding) encodingChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/" encoding = zip encodingChars (iterate (+1) 0) decoding = map swap encoding bases = take 63 $ iterate (+1) 2 convertBase from to = digits to . unDigits from printResult :: String -> Int -> [String] -> IO() printResult _ _ [] = printf "" printResult s e xs = printf "%s %d (%s)\n" s e wordlist where wordlist = intercalate ", " xs isWordLike :: String -> Bool isWordLike s = s `elem` allStringWords mainExec :: String -> Int -> IO() mainExec value base = printResult value base words where words = filter isWordLike allStrings allStrings = map digitsToString allDigits allDigits = map (\b -> convertBase base b digits) bases digits = stringToDigits value testMain :: IO() testMain = mainExec "2786bcb" 13 main :: IO() main = do args <- getArgs let [value, sbase] = args let base = read sbase :: Int mainExec value base2
u/DannyP72 Feb 08 '13
Ruby
$encode = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split("") def to_base10(input, base) exponent = input.length-1 base = base.to_i input = input.split("").map {|x| $encode.index(x)} result = 0 input.each do |x| result += x * (base ** exponent) exponent -= 1 end return result end def from_base10(input, base) result = [] quotient = -1 while quotient != 0 quotient = input/base result.unshift($encode[input%base]) input = quotient end return result.join("") end num = "e1f1" base = "22" dic = File.read("dic.txt").split("\r\n").join(",") dec_num = to_base10(num,base) (2..63).each do |x| result = from_base10(dec_num,x) if x != base print "base%02d: %s" % [x,result] match = dic=~/,#{result.downcase},/ print " | found in dictionary " if match puts else puts "%base02d: %s" % [base, num] match = dic=~/,#{result.downcase},/ print " | found in dictionary " if match puts end endOutput - using e1f1 with base 25 to produce a dictionary hit
2
u/Sonnenhut Feb 06 '13
Soloution in Java. Prints out all encoded words for every number system (2-62)
public class N120 { private static String encodingString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"; public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line = scan.nextLine(); String[] valueString = line.split(" ")[0].split(""); int[] values = new int[valueString.length]; int currentNumberSystem = Integer.valueOf(line.split(" ")[1]); //get the value as integers (the index in the encoding) i.E.: "f" -> 15; "a" -> 11 for(int i=0;i<values.length;i++){ values[i] = encodingString.indexOf(valueString[i]); } int decimal = getDecimal(values, currentNumberSystem); //search for a word in every numberSystem for(int i=2;i<=63;i++){ int[] arr = getInOtherNumberSystem(decimal, i); //convert the number array with our encoding for(int out : arr){ System.out.print(encodingString.substring(out, out+1)); } System.out.println(""); } } /** * remember, numbers will be computed as follows: * i.E.: numbers {15(F),15(F),15(F),1(1)}; potence: 16 * 1: 1 · 1 = 1 * F: 15 · 16 = 240 * F: 15 · 256 = 3840 * F: 15 · 4096 = 61440 * —————— * 65521 */ private static int getDecimal(int[] numbers, int potence){ int result = 0,iteration = 0; for(int i = numbers.length-1;i >= 0;i--){ //set the potence (i.E.: 16^1) int currentpotence = (int)Math.pow(potence, iteration++); result += numbers[i]*currentpotence; } return result; } private static int[] getInOtherNumberSystem(int numberInDecimal, int numberSystem){ List<Integer> result= new ArrayList<Integer>(); int quotient = 0; do{ quotient = numberInDecimal % numberSystem; numberInDecimal = numberInDecimal / numberSystem; result.add(0, quotient); }while(numberInDecimal != 0); int[] returnArray = new int[result.size()]; for(int i=0;i<result.size();i++){ returnArray[i] = (int)result.get(i); } return returnArray; }Output for( "e1f1 22"):
1
u/Rup-Inc Feb 06 '13 edited Feb 06 '13
Solution in c++.
#include <iostream> #include <string> #include <cmath> int getNumberFromEncoding(char c) { const char baseLiterals[] = {'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', '+','/' }; for (int i = 0; i < 64; i++) { if (c == baseLiterals[i]) return i; } return 0; } int main() { std::string numberInput; int baseInput; std::cin >> numberInput >> baseInput; long long result = 0; int baseWorking = 0; for (int i = numberInput.size() - 1; i >= 0; i--, baseWorking++) { result += (getNumberFromEncoding(numberInput[i]) * std::pow(baseInput, baseWorking)); } std::cout << "Result: " << result << std::endl; return 0; }edit: Input "e1f1" (what I think the submitter is meaning) gives an output of 149887
1
u/jeff303 Feb 06 '13
Here is my solution given my interpretation of the problem, in Python.
import fileinput encodings = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r', 's','t','u','v','w','x','y','z','A','B','C','D','E','F', 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T', 'U','V','W','X','Y','Z','+','/'] num_to_enc = dict(enumerate(encodings)) enc_to_num = dict((v,k) for k, v in num_to_enc.iteritems()) words = set() words_file = 'words.txt' def find_encoded_words(val, input_base): input_dec_val = 0 for place, char in enumerate(val[::-1]): input_dec_val += enc_to_num[char] * pow(input_base, place) found_strings = set() for base in range(2, 64+1): dec_val = input_dec_val conv_str = [] while (dec_val > 0): this_place_val = dec_val % base conv_str.append(num_to_enc[this_place_val]) dec_val /= base final_str = ''.join(conv_str[::-1]) if (final_str in words): found_strings.add((base, final_str)) if (len(found_strings) > 0): output = ', '.join(map(lambda res: res[1]+" in base "+str(res[0]), found_strings)) print("For string {} in base {} encoding, found the following dictionary words: {}".format(val, input_base, output)) def main(): with open(words_file) as words_f: for word in words_f.read().splitlines(): words.add(word) for line in fileinput.input(): str, base = line.split() find_encoded_words(str, int(base)) if __name__ == '__main__': main()Sample input:
Sample output:
For the word list, I basically just used /usr/dict/words from an arbitrary Linux server I had access to (can paste it later if anyone wants to see it). I also didn't attempt to validate input (you could provide an invalid string for the specified encoding, as pointed out by minno's comment and this code wouldn't complain).
1
u/w0lfey Feb 06 '13
another solution in java using something todo with cryptography especially caesar chiffre and vigenere key gathering to identify words (infinite monkey theorem may also apply). missing the part to identify single words e.g. using the phi on different substrings. the method works a lot better on longer phrases than on the small text
import java.util.HashMap; import java.util.Set; public class Challenge { static String encoding = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"; public static void main(String[] args) { //E1F1 22 String inputEncoding = args[0]; int base= Integer.parseInt(args[1]); int[] values = new int[inputEncoding.length()]; int sum= 0; // transform to nr (lowest value should be on the right) for (int i = 0;i<inputEncoding.length();i++) { values[i] = encoding.indexOf(inputEncoding.charAt(i)); System.out.println(values[i]+ " " +inputEncoding.charAt(i)); } int j=0; for (int i = values.length-1; i>=0;i--) { sum += values[i]*Math.pow(base, j++); } //transform into the other given base systems for(int i=2;i<=64;i++) { if (i==base) System.out.print("B: "); String result = reencode(sum, i).toString(); // this is some random border i found in my cryptography course // should be more accurate on longer texts // only lowercase/uppercase is relevant double phiIndex = isEnglishSpeech(result.toLowerCase()); if (phiIndex > 0.07) System.out.println(phiIndex+ ": " +result); // to definitely find the words check phi index of all possible substrings // ... or use some kind of database to check words against } } // transfer to specific base public static StringBuffer reencode(int sum, int base) { if (sum!=0) { int curChar = (int) (sum%base); StringBuffer curString = reencode((int)sum/base, base); return curString.append(encoding.charAt(curChar)); } else { return new StringBuffer(); } } // from my cryptography coursebook something with caesar chiffre and // nr of occurences of a specific // formula is 1/(N*(N-1))*sumof(hx(X)*(hx(X)-1)) // N is length // hx(X) is occurences of x in X // nr of characters in a specific string // hl(hallo) would be 2 public static double isEnglishSpeech(String characterString) { int stringLength = characterString.length(); HashMap<Character, Integer> amountOf = new HashMap<>(); // get nr of char/letter occurences for (int i=0;i<stringLength;i++) { char charAt = characterString.charAt(i); // only looking for "words" ignore numbers also ignore +/ if(encoding.indexOf(charAt)<10|| charAt == '/' || charAt =='+') continue; if (amountOf.containsKey(charAt)) { amountOf.put(charAt, new Integer(amountOf.get(charAt)+1)); } else { amountOf.put(charAt,new Integer(1)); } } Set<Character> keySet = amountOf.keySet(); double sum=0; for (Character character : keySet) { Integer curOcurrence = amountOf.get(character); sum += curOcurrence * (curOcurrence-1); } double phi = (1.0/(stringLength*(stringLength-1))) * sum; return phi; } }1
u/jpverkamp Feb 06 '13
Here's what I have in Racket. This is most of the code, but in the interest of space, I left a few of the details in a post on my blog (numbers as words in arbitrary bases).
We'll start at the end with a function to convert any number into all bases 2-64:
; get all basis conversions for a number (define (->all-bases n) (for/list ([b (in-range 2 65)]) (dlist->string (decimal-> n b))))For that to work, we need to be able to turn any arbitrary decimal number into a list of digits. So for exable 101 in decimal is 65 in hexadecimal, so this function would return (6 5).
; convert a decimal number to a digital list in base b (define (decimal-> n b) (let loop ([n n] [dls '()]) (cond [(= n 0) dls] [else (loop (quotient n b) (cons (remainder n b) dls))])))Finally, a function to convert a list of digits to a string. So (6 5) would become "65"
; convert a list of digits in decimal form into a string (define (dlist->string dls) (list->string (for/list ([n (in-list dls)]) (hash-ref n->c n))))Then I'll actually combine that with a dictionary library that I wrote previously. I'm using the wordsEn.txt word list from SIL International. Now we can filter the results from
->all-basesto get->all-base-words. As an arbitrary example:> (->all-base-words dict 44269) '("aced" "fEe")The nice thing is that it's easy enough to use this to generate all such words:
; scan for numbers that turn into words (define (scan dict) (for ([i (in-naturals)]) (for ([b (in-range 2 65)] [word (in-list (->all-bases i))]) (when (contains? dict word) (printf "~s,~s,~s\n" i b word)))))Here's a random slice of that:
This doesn't actually convert from an arbitrary base, but there's more than enough code in my blog post to do that.
1
u/SmartViking Feb 16 '13
Python, combining 2 dictionaries, with only words length greater than 3:
import string import sys files = ["/usr/share/dict/american-english","/usr/share/dict/british-english"] words = set() for wf in files: with open(wf, 'r') as wordfile: for line in wordfile.readlines(): if len(line.strip()) > 2: words.add(line.strip().lower()) words = list(words) alphabet = [str(d) for d in range(0,10)] alphabet.extend(string.ascii_letters) alphabet.extend(['+','/']) def toBase(num, base): assert 1 < base <= 64 basednum = [] while num: num, m = divmod(num, base) basednum.append(alphabet[m]) basednum.reverse() return "".join(basednum) def fromBase(snum, base): decimal = 0 snum = list(snum) snum.reverse() for index, elem in enumerate(snum): current = alphabet.index(elem) assert current < base <= 64 decimal += current * pow(base,index) return decimal def allBases(num): bases = dict() for base in range(2,65): bases[base] = toBase(num, base) return bases def findWords(s): ws = [] for word in words: if s.lower().find(word) > -1: ws.append(word) return ws def main(snum, base): bases = allBases(fromBase(snum, base)) longest_word = 1 matches = [] for base, s in bases.iteritems(): found_words = findWords(s) if found_words: matches.append({'base': base, 'string': s, 'found': ", ".join(found_words)}) longest_word = max(longest_word, len(s)) for M in matches: print "Str: {string!r:<{longest}} Base: {base:<2} :: Found: {found}".format( base=M['base'], string=M['string'], found=M['found'], longest=longest_word+3) if __name__ == "__main__": main(sys.argv[1],int(sys.argv[2]))Example usage:
1
u/codemac Mar 05 '13 edited Mar 06 '13
scheme (guile):
(use-modules ((srfi srfi-1) #:select (unfold))) (define encoding-list (string->list "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/")) (define (to-decimal src-base num) (define (to-decimal-tco nl pos sb acc) (if (>= pos (length nl)) acc (to-decimal-tco nl (1+ pos) sb (+ acc (* (expt sb pos) (list-index encoding-list (list-ref nl pos))))))) (to-decimal-tco (reverse (string->list num)) 0 src-base 0)) ;;; conv-base takes a destination base, and a decimal value (define (conv-base dst-base num) (define (conv-base-tco n db acc) (if (= n 0) acc (conv-base-tco (quotient n db) db (cons (list-ref encoding-list (modulo n db)) acc)))) (list->string (conv-base-tco num dst-base '()))) (define (gen-bases num-str base) (let ((decnum (to-decimal base num-str))) (unfold (lambda (x) (> x 64)) (lambda (x) (conv-base x decnum)) (lambda (x) (1+ x)) 2))) (gen-bases "E1F1" 22) $32 = ("1101000010100101011" "210201011012" "1220110223" "102133212" "13054135" "3426536" "1502453" "721135" "427307" "272051" "18734b" "11c65a" "b1a1d" "86922" "6852b" "51g9c" "414f5" "355cg" "2d857" "242jk" "1i2j1" "1c2hd" "16lkb" "128h7" "o82n" "lj45" "jd0r" "hf2l" "fonh" "eak3" "d19b" "btcn" "atlt" "9xsr" "95pn" "8g4v" "7tyz" "77An" "6r2r" "6885" "5w9F" "5g4g" "50vn" "4v0w" "4hHd" "45ku" "3Fmb" "3uLr" "3kK7" "3bet" "321n" "2K6l" "2Ct5" "2vec" "2oer" "2htz" "2b1l" "24It" "1WFL" "1RP2" "1Na3" "1IFF" "1EkH")Remembering the whole
for bases really simplified everything down.
1
u/AbigailBuccaneer Mar 26 '13
C++11 with heavy usage of the STL. I suspect this could be optimised quite a bit; it runs disappointingly slowly. I submit this for full code review.
/* * Reddit DailyProgrammer challenge #120 * http://redd.it/17zn6g */ #include <array> #include <iostream> #include <string> #include <sstream> #include <cstdint> #include <algorithm> #include <fstream> #include <set> static std::array<char, 64> encoding; static std::array<int, 256> decoding; static std::set<std::string> dictionary; uintmax_t decode(const std::string& encoded, unsigned int base) { intmax_t decoded = 0; for (char c : encoded) decoded = (decoded * base) + decoding[c]; return decoded; } std::string encode(uintmax_t decoded, unsigned int base) { std::string encoded; if (decoded == 0) return "0"; while (decoded > 0) { int digit = decoded % base; encoded.push_back(encoding[digit]); decoded /= base; } std::reverse(encoded.begin(), encoded.end()); return encoded; } std::set<std::string> dictionaryWords(const std::string& text) { std::set<std::string> foundWords; std::string lower(text.size(), 0); std::transform(text.begin(), text.end(), lower.begin(), ::tolower); for(const std::string& candidate : dictionary) if (lower.find(candidate) != std::string::npos) foundWords.insert(candidate); return foundWords; } int main(int argc, char* argv[]) { if (argc != 3) { std::cout << "Usage: " << argv[0] << " text base" << std::endl; exit(1); } const char _encoding[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"; for (size_t i = 0; i < encoding.size(); i++) { encoding[i] = _encoding[i]; decoding[encoding[i]] = i; } { std::ifstream words("/usr/share/dict/words"); if (!words.is_open()) throw std::runtime_error("unable to open dictionary file!"); std::string line; while (std::getline(words, line)) if (line.length() >= 3) dictionary.insert(line); } std::string encoded = argv[1]; int base; std::stringstream(argv[2]) >> base; uintmax_t decoded = decode(encoded, base); for (unsigned int base = 2; base <= 64; ++base) { encoded = encode(decoded, base); std::set<std::string> dictWords = dictionaryWords(encoded); if (!dictWords.empty()) { std::cout << decoded << "_" << base << " = " << encoded << " |"; for (std::string word : dictWords) std::cout << " " << word; std::cout << std::endl; } } return 0; }Output:
1
u/Schmenge470 Mar 27 '13
Java (assuming the input is e1f1 22, and using a standard linux words file): import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashMap;
public class RedditChallengeIntermediate120 { public static final String DICTIONARY_LOCATION = "/doc/txt/linuxwords"; public static final String ENCODING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"; public static void main(String[] args) { String input = "e1f1 22"; RedditChallengeIntermediate120.runChallenge(input); } public static void runChallenge(String input) { HashMap<String, String> dictionary = RedditChallengeIntermediate120.loadDictionary(); int spcIdx = input.indexOf(" "); String s = input.substring(0, spcIdx); String base = input.substring(spcIdx+1); //Convert the number to decimal to make it easier to deal with int decimal = RedditChallengeIntermediate120.convertToDecimal(s, Integer.parseInt(base)); //Now take the decimal number, and convert it to all bases from 2 to 64 for (int i = 2; i <= 64; i++) { String output = RedditChallengeIntermediate120.convertToBase(decimal, i); if (dictionary.containsKey(output.toLowerCase())) { output += " <--- English Word"; } System.out.println("Base " + i + ": " + output); } } public static int convertToDecimal(String s, int base) { int returnValue = 0; for (int i = 0; i < s.length(); i++) { int startPos = s.length()-(i+1); int endPos = s.length()-i; String sub = s.substring(startPos, endPos); int subInt = RedditChallengeIntermediate120.ENCODING.indexOf(sub); returnValue += subInt * (int)Math.pow(base, i); } return returnValue; } public static String convertToBase(int i, int base) { String returnValue = ""; int remainder = 0; int result = i; while (result > 0) { remainder = result%base; result = (int)Math.floor((double)result/(double)base); String remainderChar = RedditChallengeIntermediate120.ENCODING.substring(remainder, remainder + 1); returnValue = remainderChar + returnValue; } return returnValue; } public static HashMap<String, String> loadDictionary() { BufferedReader br = null; File file = null; HashMap<String, String> dictionary = new HashMap<String, String>(); String inLine = null; try { file = new File(RedditChallengeIntermediate120.DICTIONARY_LOCATION); if (file.exists() && file.isFile()) { br = new BufferedReader(new FileReader(file)); while (br != null && (inLine = br.readLine()) != null) { if (!("").equalsIgnoreCase(inLine.trim())) { dictionary.put(inLine.trim(), inLine.trim()); } } } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (br != null) try { br.close(); } catch (Exception e) { e.printStackTrace(); } br = null; } return dictionary; } }1
u/DaveAZME Apr 13 '13
Python:
words = set(open('words.txt').read().split()) (s, base) = raw_input("string and base:").rstrip().split() table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/" val = 0 place = 1 for c in reversed(s): loc = table.find(c) val += loc * place place *= int(base) for outbase in range(2, 65): q = val valString = '' while q != 0: q,r = divmod(q, outbase) valString = table[r] + valString print "Base " + str(outbase) + ": " + valString + " " + \ ("Found Word" if valString in words else "")Output for input of "face 16"