diff --git a/Challenge questions/shubham2704/challenge6/README.md b/Challenge questions/shubham2704/challenge6/README.md new file mode 100644 index 0000000..8ed86c4 --- /dev/null +++ b/Challenge questions/shubham2704/challenge6/README.md @@ -0,0 +1 @@ +solving challenge 6 in challenge section diff --git a/Challenge questions/shubham2704/challenge6/num2words.py b/Challenge questions/shubham2704/challenge6/num2words.py new file mode 100644 index 0000000..2811873 --- /dev/null +++ b/Challenge questions/shubham2704/challenge6/num2words.py @@ -0,0 +1,41 @@ +def numToWords(num,join=True): + '''words = {} convert an integer number into words''' + units = ['','one','two','three','four','five','six','seven','eight','nine'] + teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen', \ + 'seventeen','eighteen','nineteen'] + tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy', \ + 'eighty','ninety'] + thousands = ['','thousand','million','billion','trillion','quadrillion', \ + 'quintillion','sextillion','septillion','octillion', \ + 'nonillion','decillion','undecillion','duodecillion', \ + 'tredecillion','quattuordecillion','sexdecillion', \ + 'septendecillion','octodecillion','novemdecillion', \ + 'vigintillion'] + words = [] + if num==0: words.append('zero') + else: + numStr = '%d'%num + numStrLen = len(numStr) + groups = (numStrLen+2)/3 + numStr = numStr.zfill(groups*3) + for i in range(0,groups*3,3): + h,t,u = int(numStr[i]),int(numStr[i+1]),int(numStr[i+2]) + g = groups-(i/3+1) + if h>=1: + words.append(units[h]) + words.append('hundred') + if t>1: + words.append(tens[t]) + if u>=1: words.append(units[u]) + elif t==1: + if u>=1: words.append(teens[u]) + else: words.append(tens[t]) + else: + if u>=1: words.append(units[u]) + if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',') + if join: return ' '.join(words) + return words + + +print(numToWords(0)) +print(numToWords(11)) \ No newline at end of file diff --git a/Challenge questions/shubham2704/challenge6/textToNum.py b/Challenge questions/shubham2704/challenge6/textToNum.py new file mode 100644 index 0000000..9d06a4f --- /dev/null +++ b/Challenge questions/shubham2704/challenge6/textToNum.py @@ -0,0 +1,62 @@ +def text2num (textnum, numwords={}): + if not numwords: + units = [ + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", + "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", "seventeen", "eighteen", "nineteen", + ] + + tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] + + scales = ["hundred", "thousand", "million", "billion", "trillion"] + + numwords["and"] = (1, 0) + for idx, word in enumerate(units): numwords[word] = (1, idx) + for idx, word in enumerate(tens): numwords[word] = (1, idx * 10) + for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0) + + ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12} + ordinal_endings = [('ieth', 'y'), ('th', '')] + + textnum = textnum.replace('-', ' ') + + current = result = 0 + curstring = "" + onnumber = False + for word in textnum.split(): + if word in ordinal_words: + scale, increment = (1, ordinal_words[word]) + current = current * scale + increment + if scale > 100: + result += current + current = 0 + onnumber = True + else: + for ending, replacement in ordinal_endings: + if word.endswith(ending): + word = "%s%s" % (word[:-len(ending)], replacement) + + if word not in numwords: + if onnumber: + curstring += repr(result + current) + " " + curstring += word + " " + result = current = 0 + onnumber = False + else: + scale, increment = numwords[word] + + current = current * scale + increment + if scale > 100: + result += current + current = 0 + onnumber = True + + if onnumber: + curstring += repr(result + current) + + return curstring + +print(text2num("one two three")) +print(text2num("five forty five")) +print(text2num("there are seven wonders and nine planets")) +