Originally published by Vatsal Mistry at <a href="https://dev.to/mistryvatsal/how-easy-type-conversion-in-python-helped-me-a-solution-to-problem-from-code-jam-2019-573d" target="_blank">Dev.to</a> on Apr 18
— A problem from Google Code Jam's Qualifier Round this year felt like so easy to code.
You can checkout the full problem description at this LINK.Problem(Source : Google Code Jam 2019 Website)
Someone just won the Code Jam lottery, and we owe them N jamcoins! However, when we tried to print out an over sized check, we encountered a problem. The value of N, which is an integer, includes at least one digit that is a 4... and the 4 key on the keyboard of our over sized check printer is broken.
Fortunately, we have a workaround: we will send our winner two checks for positive integer amounts A and B, such that neither A nor B contains any digit that is a 4, and A + B = N. Please help us find any pair of values A and B that satisfy these conditions.Approach
While reading out the problem description, I read the input constraints and range which were around 10¹⁰⁰. So it was a bad idea to think of a very straight loop-based solution, that would strike the time complexity showing the nightmarish message “ Time limit Exceeded ! ”.
So I thought of dealing this with the help of strings. Python saved me!
Assuming you’ve read the problem description through the link above, there is a number N which may contain the digit 4 with any number of occurrences. The task was to find out two numbers A and B such that A and B doesn’t contain any occurrence of the digit 4 and summation of A and B should equal N(A+B=N).
So I thought of creating a new number let’s assume A for now, that would be same in length as N(length means number of digits in the number). The idea is while constructing the number A, we would analyze the number N digit by digit. For each occurrence of digit 4 in number N, we would add digit 1 in the number A. For rest of the digits in N, add digit 0 in the A. Following this, I got a number A.
I obtained the other number B by subtracting newly constructed A from N. And you see, I got two numbers A and B that do not contain digit 4 and the summation is equal to N.
For implementing this logic, I could not think of anything, but Python. The easy type conversions from integer to string or string to list and viz made it so easy to achieve this working.Implementation
# __author__ = "Vatsal Mistry" # A solution for FOREGONE SOLUTION Problem from Qualifier Round of Google's CodeJam 2019. # Problem Description Link : https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/0000000000088231 # Importing regular expression module. import re # tc holds the number of testcases. tc = int(input()) # A loop over tc i.e. number of testcases. for i in range(tc): # N holds the input number. N = int(input()) # Converting the integer N into string N_str. N_str = str(N) # Defining an empty list index_list. index_list = list() # Looping over N_str for finding the indices of positions where the digit "4" occurs. # finditer method from re module is used. It returns the indices where match is found. for match in re.finditer("4", N_str): # Adding the indices to the index_list defined above. index_list.append(match.start()) # Creating a new empty list new_number_list. new_number_list = list() #The following block is the logic to create a new number with the rules that whereever there occurs a digit 4 in the original number there will be digit 1 in the new number, rest the digit will be 0. # Loop to create new number, each digit in list. for j in range(len(N_str)): if j not in index_list: new_number_list.append("0") else: new_number_list.append("1") # Converting the new number list into string. new_number = int("".join(new_number_list)) # Defining and assigning as per given condition in problem. A = new_number B = N - new_number # Printing result. print("Case #" + str(i+1) + ":" + " " + str(A) + " " + str(B))
This way it cracked all the test cases, no matter how large the input number was!
I believe the same logic can be implemented in various other languages, possibly the best in terms of time would be in C/C++, but Python just makes it super easy. You can switch from different data types with just few keywords and as you want.
I encourage any reader to try out this in some other language or in Python with less lines of code.
Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.
We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.
Know more about Top E-Commerce Web Development Company
Python tutorial for beginners - Learn Python for Machine Learning and Web Development. Can Python be used for machine learning? Python is widely considered as the preferred language for teaching and learning ML (Machine Learning). Can I use Python for web development? Python can be used to build server-side web applications. Why Python is suitable for machine learning? How Python is used in AI? What language is best for machine learning?Python tutorial for beginners - Learn Python for Machine Learning and Web Development
TABLE OF CONTENT
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...
PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]