We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #17: Number to Words
Project Euler #17: Number to Words
Contest ends in
+ 0 comments d1 = {1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"} d2 = {10: "Ten", 11: "Eleven", 12: "Twelve", 13: "Thirteen", 14: "Fourteen", 15: "Fifteen", 16: "Sixteen", 17: "Seventeen", 18: "Eighteen", 19: "Nineteen"} d3 = {2: "Twenty", 3: "Thirty", 4: "Forty", 5: "Fifty", 6: "Sixty", 7: "Seventy", 8: "Eighty", 9: "Ninety"} def number1(n): # this fonction give the numbre with word with number between 0 nd 100 if 1 <= n < 10: return d1[n] elif 10 <= n < 20: return d2[n] else: a = n % 10 b = n // 10 if a == 0: return d3[b] else: ch = d3[b] + " " + d1[a] return ch def number2(n): # this fonction give the numbre with word with number between 0 nd 999 if n < 100: return number1(n) else: a = n % 100 b = n // 100 if a == 0: return d1[b] + " Hundred" else: ch = d1[b] + " Hundred " + number1(a) return ch d4 = {1: "Thousand", 2: "Million", 3: "Billion", 4: "Trillion"} def number3(n): # this fonction give the numbre with word with number between 0 nd 9999.. if n < 1000: return number2(n) else: ch1 = str(n) l = [] for i in range(len(ch1), 0, -3): l.append(int(ch1[max(i - 3, 0):i])) ch2 = number2(l[0]) for i in range(1, len(l)): if l[i] != 0: ch2 = number2(l[i]) + " " + d4[i] + " " + ch2 return ch2 for _ in range(int(input())): num = int(input()) print(number3(num))
+ 0 comments Converts a vowel number to its English word representation code js
+ 0 comments Here is my c# code with 100 points
using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; class Solution { static string[] digits = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; static string[] tens = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; static (long, string)[] other = { (1000000000000, "Trillion"), (1000000000, "Billion"), (1000000, "Million"), (1000, "Thousand"), (100, "Hundred") }; static string GetWord(long n) { string result = ""; foreach ((long value, string name) in other) { long tmp = n / value; if (tmp > 0) { result += GetWord(tmp) + name; n -= tmp * value; } } for (int t = tens.Length - 1; t >= 0; t--) { long tmp = n / ((t + 2) * 10); if (tmp > 0) { result += tens[t]; n -= tmp * (t + 2) * 10; break; } } if (n > 0) { result += digits[n - 1]; } return result; } static void Main(String[] args) { int t = Convert.ToInt32(Console.ReadLine()); // Input the number of test cases for (int i = 0; i < t; i++) { long n = Convert.ToInt64(Console.ReadLine()); // Input the value of n string s = GetWord(n); string[] l = Regex.Split(s, "(?<=.)(?=[A-Z])"); string res = string.Join(" ", l); Console.WriteLine(res); } } }
+ 0 comments It was an interesting question, here's my solution in C++:
#include <bits/stdc++.h> using namespace std; map<long long, string> spell; const long long int tri = 1e12; const long long int bil = 1e9; const long long int mil = 1e6; void precomp(){ spell[0] = "Zero"; spell[1] = "One"; spell[2] = "Two"; spell[3] = "Three"; spell[4] = "Four"; spell[5] = "Five"; spell[6] = "Six"; spell[7] = "Seven"; spell[8] = "Eight"; spell[9] = "Nine"; spell[10] = "Ten"; spell[20] = "Twenty"; spell[30] = "Thirty"; spell[40] = "Forty"; spell[50] = "Fifty"; spell[60] = "Sixty"; spell[70] = "Seventy"; spell[80] = "Eighty"; spell[90] = "Ninety"; spell[100] = "Hundred"; spell[1000] = "Thousand"; spell[1e6] = "Million"; spell[1e9] = "Billion"; spell[1e12] = "Trillion"; spell[11] = "Eleven"; spell[12] = "Twelve"; spell[13] = "Thirteen"; spell[14] = "Fourteen"; spell[15] = "Fifteen"; spell[16] = "Sixteen"; spell[17] = "Seventeen"; spell[18] = "Eighteen"; spell[19] = "Nineteen"; } string toWords(long long n){ if(n==0) return spell[0]; string sp = " "; string ans; long long k ; //trillion if(n == 1e12) ans.append(spell[1] + sp + spell[1e12] + sp); else { //Billion if((n%tri) >= 1e9){ k = n; k/=(1e9); if(k/100){ ans.append(spell[k/100]+sp); ans.append(spell[100]+sp);} if(k%100 > 10 && k%100 < 20){ ans.append(spell[k%100] + sp); } else if(k%100 >=20){ ans.append(spell[k%100 - (k%100)%10]+sp); if(k%10) ans.append(spell[k%10]+sp); } else if(k%100 > 0) ans.append(spell[k%100]+sp); ans.append(spell[1e9]+sp); } //million if((n%bil)>=1e6){ k = n%bil; k/=(1e6); if(k/100){ ans.append(spell[k/100]+sp); ans.append(spell[100]+sp);} if(k%100 > 10 && k%100 < 20){ ans.append(spell[k%100] + sp); } else if(k%100 >=20){ ans.append(spell[k%100 - (k%100)%10]+sp); if(k%10) ans.append(spell[k%10]+sp); } else if(k%100 > 0) ans.append(spell[k%100]+sp); ans.append(spell[1e6]+sp); } if((n%mil)>=1e3){ k = n%mil; k/=(1e3); if(k/100){ ans.append(spell[k/100]+sp); ans.append(spell[100]+sp);} if(k%100 > 10 && k%100 < 20){ ans.append(spell[k%100] + sp); } else if(k%100 >=20){ ans.append(spell[k%100 - (k%100)%10]+sp); if(k%10) ans.append(spell[k%10]+sp); } else if(k%100 > 0) ans.append(spell[k%100]+sp); ans.append(spell[1e3]+sp); } k=n%1000; if(k/100){ ans.append(spell[k/100]+sp); ans.append(spell[100]+sp);} if(k%100 > 10 && k%100 < 20){ ans.append(spell[k%100] + sp); } else if(k%100 >=20){ ans.append(spell[k%100 - (k%100)%10]+sp); if(k%10) ans.append(spell[k%10]+sp); } else if(k%100 > 0) ans.append(spell[k%100]+sp); } return ans; } int main(){ precomp(); int t; cin >> t; long long n; string sp = " "; while(t--){ cin >> n; cout << toWords(n) << '\n'; } return 0;}
+ 0 comments I spent like 30 minutes debugging my code. I thought I messed up spaces between the words. I rechecked every single word, remade the way I place spaces like 5 times. In reality, I messed up "Forty". It was written as "Fourty". I think, these types of problems shoud include the "Right" way of spelling words. Or at least have these kind of words in example runs.
digits = ["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"] other = [(10**12, "Trillion"), (10**9, "Billion"), (10**6, "Million"), (1000, "Thousand"), (100, "Hundred")] import re def get_word(n): result = "" for t in other: tmp = n // t[0] if (tmp > 0): result += get_word(tmp) + t[1] n -= (tmp * t[0]) for t in range(len(tens)-1, -1, -1): tmp = n // ((t + 2) * 10) if (tmp > 0): result += tens[t] n -= (tmp * (t+2) * 10) break for t in range(len(digits)): if (n == (t+1)): result += digits[t] break return result for _ in range(int(input())): n = int(input()) s = get_word(n) l = re.split('(?<=.)(?=[A-Z])', s) res = "" for i in l: res += i + " " print(res[:-1])
Load more conversations
Sort 82 Discussions, By:
Please Login in order to post a comment