Python puzzles to improve yourself as a junior coder




Been a python coder for some time, I do find programming puzzles as a very interesting way to keep my brain in shape and also what's most important, to improve my coding skills. Having contact and friendship with veteran python developers, I have come to the conclusion that critical thinking and algorithmic solving skills are a must for a professional software programmer.

 

With the main purpose of keeping myself updated with the latest python programming puzzles, I decided to compile the whole list of the algorithmic problems that I put my hands on. I do hope this blog post serves as a humble guide for anyone who is interested in improving their python coding skills.

 

Note: The solutions are purely based on my own way of thinking, they may be not as professional as it is required. Feel free to comment so I can update them.


Puzzle 1

Define a function which takes an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".

Solution


Based on simple arithmetic, an even number returns 0 for the remainder when divided by 2. For example, 4/2 =2 and the remainder is 0. With such logic, we can easily find out if the remainder is 0 or not by making usage of the builtin python operator % . 






Puzzle 2


Find the minimum of an array without using builtin, then find the maximum.

Solution

First, we setup the first element of the array as the minimum. Then, with the help of a for loop, we find the next minimum, until we have looped the whole array.


We do the opposite for finding the maximum.


Puzzle 3


Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are the square of the keys.

Solution


First we define the keys of the dictionary inside a list. Then with the help of a for loop, we assign the key value pairs to the dictionary.



Puzzle 4




Write a program to generate and print another tuple whose values are even numbers in the given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10).

Solution


The trick in here is to store the even numbers inside a list object and once finished the population, to convert it into a tuple object.


Puzzle 5


Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No".

Solution

The key in here is to store the strings "yes", "Yes" and "YES" inside a list. Then do a check for the input within the list.


Puzzle 6


Write a function to compute 5/0 and use try/except to catch the exceptions. 

Solution






Puzzle 7


Make use of the assert statement to verify that every number in the list [2, 4, 6, 8] is even.

Solution


Puzzle 8


Add two numbers without using the addition operator.

Solution


A direct idea is to make usage of the subtract operator. That would work only for the positive numbers, so it's not a solution. The following is a better approach.


Puzzle 9


Sort an array without using the builtin method. [13, 14, 11, 2, 1] --> [1, 2, 11, 13, 14]

Solution


My idea of solving this puzzle is to create a fresh empty list and then populate it with each minimum of the given array.



As you can see from the solution provided above, we make use of the find_min from the second puzzle.


Puzzle 10


Write a program which accepts a sentence and calculates the number of uppercase letters and lowercase letters. Suppose the following input is provided to the program:
Hello World!
Then the output should be:
UPPER CASE 2
LOWER CASE 9

Solution





Puzzle 11


Write a program to compute the factorial of a given number.

Solution


The builtin range function becomes useful in our case. We compute the list of factors with the help of it, and then we generate the factorial like shown in the following piece of code.



Although the above algorithm works fine, the following solution is a more professional and elegant one.


Puzzle 12


Write a program that computes a value of a + aa + aaa + aaaa with a given digit as the value of a. Suppose the following input is supplied to the program: 
9
Then, the output should be: 
11106

Solution



Puzzle 13


Write a program which will find all the numbers which are divisible by 7 but are not a multiple of  5, between 2000 and 3200. The numbers should be printed in a comma-separated sequence on a single line.

Solution




Puzzle 14


Write a program to generate all the sentences where subject is in ["I", "You"], verb is in ["Play", "Love"] and the object is in ["Hockey", "Football"].

Solution


The solution to this puzzle involves multiple for loops.



Puzzle 15


Write a program which accepts a sequence of comma separated values from the console and generates a list and a tuple which contains every number. Suppose the following input is supplied to the program:

34, 67, 55, 33, 12, 98

Then the output should be:

[34, 67, 55, 33, 12, 98]
(34, 67, 55, 33, 12, 98)

Solution



Puzzle 16


Define a function that can accept two strings as input and print the string with maximum length on the console. If two strings have the same length, then the function should print both the strings line by line.

Solution


The if, else, elif conditionals can be easily implemented for the solution of this puzzle.




Puzzle 17


Write a program to filter even numbers in a list using the filter function. The list is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. 

Solution


For those of you who are not aware of the filter function, it is a builtin utility which takes for arguments a function and an iterable with the main purpose of filtering any element which proves True to the provided function.


Puzzle 18


When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise.

cigar_party(30, False)->False
cigar_party(50, False)->True
cigar_party(70, True)->True
cigar_party(70, False)->False

Solution




Puzzle 19


Given two ints a, and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20.

Solution





Puzzle 20


The number 6 is truly a great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. 

# love6(6, 4)-->True
# love6(4, 5)-->False
# love6(1, 5)-->True

Solution


The abs builtin functionality becomes useful in such case.




Puzzle 21


Given three int values, a b c, return their sum. However, if one of the values is 13, then it does not count towards the sum and the values to its right do not count. So for example, if b is 13 then both b and c do not count.

Solution




Puzzle 22


For this problem, we'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20.  Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. Given 3 ints, a b c, return the sum of their rounded values. To avoid code repetition, write a separate helper "def round10(num):" and call it 3 times.

Solution





Puzzle 23



Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive-- then that value counts as 0, except 15 and 16 do not count as teens. Write a separate helper "def fix_teen(n):" that takes n as an int value and returns that value fixed for the teen rule.

# no_teen_sum(1, 2, 3)-->6
# no_teen_sum(2, 13, 1)-->3
# no_teen_sum(2, 1, 14-->3


Solution



Puzzle 24


Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count toward the sum.


# lone_sum(1, 2, 3)->6
# lone_sum(3, 2, 3)->2
# lone_sum(3, 3, 3)->0

Solution





Puzzle 25



We want to make a package of goal kilos of chocolate. We have  small bars (1 kilo each) and big bars (5 kilos each).Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.

# make_chocolate(4, 1, 9)-->4
# make_chocolate(4, 1, 10)-->-1
# make_chocolate(4, 1, 7)--> 2
# make_chocolate(6, 1, 10)-->5

Solution




Puzzle 26


We want to make a row of bricks that is goal inches long. We have a number of small bricks(1 inch each) and big bricks(5 inches each). Return True if it is possible to make the goal by choosing from the given bricks.


# make_bricks(3, 1, 8)-->True
# make_bricks(3, 1, 9)-->False
# make_bricks(3, 2, 10)--True

Solution




Puzzle 27


You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes.The result getting the table is encoded as int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that either of you has style of 2 or less, then the result is 0(no).
Otherwise the result is 1 (maybe).

# date_fashion(5, 10)->2
# date_fashion(5, 2)->0
# date_fashion(5, 5)->1

Solution



Puzzle 28


The squirrels in Palo Alto spend most of the day playing.In particular, they playif the temperature is between 60 and 90. Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean  is_summer, return True if the squirrels play and False otherwise.

# squirrel_play(70, False)->True
# squirrel_play(95, False)->False
# squirrel_play(95, True)->True

Solution




Puzzle 29


Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue,...6=Sat, and a boolean indicating if we are
on a vacation, return a string of the form "7:00" indicating when the alarm clock should ring.Weekdays,
the arlam should be "7:00" and on the weekend it should be "10:00".Unless we are on vacation--then on weekdays it should be "10:00" and weekends it should be "off".


# alarm_clock(1, False)->'7:00'
# alarm_clock(5, False)->'7:00'
# alarm_clock(0, False)->'10:00'

Solution




Puzzle 30


Given a number n, return True if n is in the range 1..10, inclusive.Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.

Solution





Final thoughts


None of the great professional coders made it to the top without hard work. It is a fact. Without great expectations, consider the puzzles shared through this blog post as a way to keep your brain in shape, and also as a first step in setting a corner stone for your coder journey.

Although there are exactly 30 puzzles shared in here, I am going to add more in the near future. Feel free to contact me, or comment below, if you have any helpful suggestion for the future puzzles. 

Feel free to follow my work on Instagram.

 
© 2020 Copyright by orthodoxpirate.blogspot.com
All Rights Reserved

2 comments:

  1. There is a more pythonic way to solve exercise number 4 via List Comprehension.

    I made a Jupyter Notebook with a full tutorial on the matter, covering the very basics to more advanced features. You may find it here: https://github.com/dan-almenar/Python-List-Comprehension-Tutorial

    ReplyDelete
    Replies
    1. I totally agree. It's a more pythonic way and also saves many lines of code. I am going to update the solution. If you have any feedback on the other puzzles, feel free to share it.

      Delete

Powered by Blogger.