problem solving | Sololearn: Learn to code for FREE!

+4

problem solving

there are some problems in solo learn to complete easy,, but i have some problems see this problem๐Ÿ‘‡ You write a phrase and include a lot of number characters (0-9), but you decide that for numbers 10 and under you would rather write the word out instead. Can you go in and edit your phrase to write out the name of each number instead of using the numeral? how i can get strings that sololearn problem automatically give to test my code??.i write my algorithm and works perfect, the problem is how to get the test phrases of solo๐Ÿ™„๐Ÿค” pls help me,๐Ÿ˜ค๐Ÿ˜ค๐Ÿ˜ค

3/7/2020 1:17:26 PM

Ali Najafian

20 Answers

New Answer

+3

Just do it as if you are taking input from the user. For example in cin(in C++),input()(in python),scanf()(in C)

+2

no, problem is that, user wont enter the phrase, the sololearn has 6 test case, that they are fix each test case has one input!!!

+2

Ali Najafian you can consider sololearn as a person who is going to run your program 5 times and input an phrase as input everytime

+2

Is it test case 3 ?

+2

import string def solve(s): for ch in s: if ch == '1': print("one", end = '') elif ch == '2': print("two", end = '') elif ch =='3': print("three", end = '') elif ch == '4': print('four', end = '') elif ch == '5': print('five',end = '') elif ch == '6': print( 'six', end = '') elif ch == '7': print('seven', end = '') elif ch == '8': print('eight', end = '') elif ch == '9': print('nine', end = '') elif ch == '0': print('zero', end = '') elif ch == '10': print('ten', end ='') else: print(ch , end='') s=input() solve(s)

+1

thnaks, solved!! but one test case failde!!๐Ÿค” and it is locked i can not see result, how i can solve and debuge that, any idea?

+1

+1

It must be because your program is not converting 10 to "ten"

+1

Share your code here

+1

i think problem is 10 my code mistake๐Ÿคช

+1

ch =='10' is wrong phrase!๐Ÿ™ˆ๐Ÿ˜€

+1

Here I have fixed it๐Ÿ‘‡ https://code.sololearn.com/c4VPoOhKN01F/?ref=app

+1

+1

my code was so bad what's your idea? i think i could write it using 2 nested iteration through a dictionary and input steing, and solve that faster, is it faster or first one?

+1

Continuation: else if(str.at(i) == '7'){ str.erase(i,1); str.insert(i,"seven"); } else if(str.at(i) == '8'){ str.erase(i,1); str.insert(i,"eight"); } else if(str[i] == '9'){ str.erase(i,1); str.insert(i,"nine"); } } bool isReplaced = ReplaceString(str,"onezero","ten"); if(isReplaced){ cout<<str; } else{ cout<<str; } } bool ReplaceString(string &s, const string &oldSubstr, const string &newSubstr){ bool isReplaced = false; int currentPosition = 0; while(currentPosition < s.length()){ int position = s.find(oldSubstr, currentPosition); if(position == string::npos){ return isReplaced; } else{ s.replace(position,oldSubstr.length(),newSubstr); currentPosition = position + newSubstr.length(); isReplaced = true; } } return isReplaced; }

0

yes, i did it, but didn't passed

0

no idea๐Ÿ™„๐Ÿ™ƒ๐Ÿค”

0

Here's my code in Java I used String and StringBuilder class to display the output Scanner s = new Scanner(System.in); System.out.print("Input: "); String strn = s.nextLine(); StringBuilder str = new StringBuilder(strn); for(int i = 0; i < str.length(); i++){ if(str.charAt(i) == '0'){ str.deleteCharAt(i); str.insert(i,"zero"); } else if(str.charAt(i) == '1'){ str.deleteCharAt(i); str.insert(i,"one"); } else if(str.charAt(i) == '2'){ str.deleteCharAt(i); str.insert(i,"two"); } else if(str.charAt(i) == '3'){ str.deleteCharAt(i); str.insert(i,"three"); } else if(str.charAt(i) == '4'){ str.deleteCharAt(i); str.insert(i,"four"); } else if(str.charAt(i) == '5'){ str.deleteCharAt(i); str.insert(i,"five"); } else if(str.charAt(i) == '6'){ str.deleteCharAt(i); str.insert(i,"six"); }

0

Here's the continuation: else if(str.charAt(i) == '7'){ str.deleteCharAt(i); str.insert(i,"seven"); } else if(str.charAt(i) == '8'){ str.deleteCharAt(i); str.insert(i,"eight"); } else if(str.charAt(i) == '9'){ str.deleteCharAt(i); str.insert(i,"nine"); } } String st = str.toString(); if(st.contains("onezero")){ System.out.print(st.replaceAll("onezero","ten")); } else{ System.out.print(st); }

0

Here's my code in C++: #include <iostream> #include <string> using namespace std; bool ReplaceString(string &s, const string &oldSubstr, const string &newSubstr); int main(){ cout<<"Input: "; string str; getline(cin,str); for(int i = 0; i < str.length(); i++){ if(str[i] == '0'){ str.erase(i,1); str.insert(i,"zero"); } else if(str[i] == '1'){ str.erase(i,1); str.insert(i,"one"); } else if(str[i] == '2'){ str.erase(i,1); str.insert(i,"two"); } else if(str[i] == '3'){ str.erase(i,1); str.insert(i,"three"); } else if(str.at(i) == '4'){ str.erase(i,1); str.insert(i,"four"); } else if(str.at(i) == '5'){ str.erase(i,1); str.insert(i,"five"); } else if(str.at(i) == '6'){ str.erase(i,1); str.insert(i,"six"); }