Python, being a very dynamic and versatile programming language, is used in almost every field. From software development to machine learning, it covers them all. This article will focus on some interesting coding problems which can be used to sharpen our skills a bit more and at the same time, have fun solving these list of specially curated problems. Although this article will focus on solving these problems using Python, one can feel free to use any other language of their choice. So let’s head right into it!
Infinite Monkey Theorem
The theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare. Well, suppose we replace a monkey with a Python function. How long would it take for a Python function to generate just one sentence? The sentence we will go for is: “a computer science portal for geeks”.
The way we will simulate this is to write a function that generates a string that is 35 characters long by choosing random letters from the 26 letters in the alphabet plus space. We will write another function that will score each generated string by comparing the randomly generated string to the goal. A third function will repeatedly call generate and score, then if 100% of the letters are correct we are done. If the letters are not correct then we will generate a whole new string. To make it easier to follow, our program should keep track of the best string generated so far.
pxwvkdfwpbzneycy rifcrnczxqtsfowgjm wfgytnakffjty ggfy trylljfhurazyxow docujzolvswarzqszridmejyluhwviujlkj qbtvqanrbwsximmnlhjgkaacufhskiooxm w jnlhvvinzrlimtesllsroqqqf wwteela mjcevludro yoigewqudxjsad bxrl qnlv f pomksbzrjizegcjwyoqftjz wwx ges
Here, we wrote three functions. One will generate a random string using the 26 characters of the alphabet and space. The second function will then score the generated string by comparing each letter of it with the
goalString. The third function will repeatedly call the first two functions until the task is completed. It will also take note of the best string generated so far by comparing their scores. The one with the highest score will be the best one. Finally, we run this program in our IDE and see it work.
The substring dilemma
This is a really interesting program as it generates some really funny outputs. It is also a healthy practice problem for beginners who want to understand the “string” type more clearly. Let’s look into the problem.
Given a string, find a substring based on the following conditions:
- The substring must be the longest one of all the possible substring in the given string.
- There must not be any repeating characters in the substring.
- If there is more than one substring satisfying the above two conditions, then print the substring which occurs first.
- If there is no substring satisfying all the aforementioned conditions then print -1.
Although there can be many methods of approach to this problem, we will look at the most basic one.
Here, we write a single function that will carry out the entire task. First it will initialize variables called
testList to store the substring and a list of possible outputs respectively. Then it will loop over the entire string provided and break every time it finds a repetition and appends that word to
testList. Finally, the longest word out of the possible outputs is returned.
racte standf cas
A low-level implementation of the classic game “Mastermind”. We need to write a program that generates a four-digit random code and the user needs to guess the code in 10 tries or less. If any digit out of the guessed four-digit code is wrong, the computer should print out “B”. If the digit is correct but at the wrong place, the computer should print “Y”. If both the digit and position is correct, the computer should print “R”. Example:
First, we write a function to generate a random four digit code using Python’s
random module. Next, we define a function that asks for user input. Finally, we write a function that compares the generated code to the guessed code and gives appropriate results.
A very simple problem with many different solutions, but the main aim is to solve it in the most efficient way. A man was given directions to go from point A to point B. The directions were: “SOUTH”, “NORTH”, “WEST”, “EAST”. Clearly “NORTH” and “SOUTH” are opposite, “WEST” and “EAST” too. Going to one direction and coming back in the opposite direction is a waste of time and energy. So, we need to help the man by writing a program that will eliminate the useless steps and will contain only the necessary directions.
For example: The directions [“NORTH”, “SOUTH”, “SOUTH”, “EAST”, “WEST”, “NORTH”, “WEST”] should be reduced to [“WEST”]. This is because going “NORTH” and then immediately “SOUTH” means coming back to the same place. So we cancel them and we have [“SOUTH”, “EAST”, “WEST”, “NORTH”, “WEST”]. Next, we go “SOUTH”, take “EAST” and then immediately take “WEST”, which again means coming back to the same point. Hence we cancel “EAST” and “WEST” to giving us [“SOUTH”, “NORTH”, “WEST”]. It’s clear that “SOUTH” and “NORTH” are opposites hence canceled and finally we are left with [“WEST”].
In the above solution, we create a dictionary of opposites to help us determine if a given direction is opposite of the other. Next we initialize a variable called
finalDirections which will be our output. If the direction which is in the
givenDirections is opposite of the last element in
finalDirections, we pop it out of
finalDirections otherwise we append it to
This problem helps one to understand the key concepts of an array(list) in Python. Two arrays are said to be the same if they contain the same elements and in the same order. However, in this problem, we will compare two arrays to see if they are same, but with a slight twist. Here, two arrays are the same if the elements of one array are squares of elements of other arrays and regardless of the order. Consider two arrays a and b.
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]
Here b can be written as:
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]
which is a square of every element of a. Hence, they are same. If a or b are
None, our program should written
- Intermediate fields in Django | Python
- 7 Cool Python Project Ideas for Intermediate Developers
- Python Tricks for Competitive Coding
- Why is python best suited for Competitive Coding?
- PEP 8 : Coding Style guide in Python
- Python3 Intermediate Level Topics
- PyQt5 – Background color of intermediate Check Box
- PyQt5 – Set Skin to intermediate CheckBox when pressed
- PyQt5 - Skin to intermediate CheckBox when mouse hover
- PyQt5 – Intermediate Check Box background when mouse hovering
- PyQt5 – Background image of intermediate indicator in Check Box
- PyQt5 – Background color of intermediate indicator in Check Box
- PyQt5 – Background image of indicator when hover over intermediate Check Box
- PyQt5 – Background color for pressed check box in intermediate state
- PyQt5 – Setting background color to pressed indicator from intermediate state | Check Box
- PyQt5 – Setting background color of indicator when mouse hover over intermediate Check Box
- PyQt5 – Background image to pressed indicator when pressed from intermediate state | Check Box
- Secure coding - What is it all about?
- Problems not solved at the end of Nth day
- TCS National Qualifier 2 Coding Question.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : user_2pui