45  Questions to solve

Next: Check 2022 list questions IMDB movie list

45.1 Simple Hello World

Please write a simple hello world example with saying your full name and your country of origin.

45.2 Input usage 1

Ask user for their name, birthday and country information. Then print this information in the screen. You will use built-in function input for this question.

45.3 Input and Print 1

Ask the user following inputs in different lines.

- Their name
- Their birth year
- Their country

An example input

- Atilla
- 1977
- Türkiye

Then calculate their age and output to screen (print) following.

Hello Atilla, you are 46 years of old and you are from Türkiye.

45.4 simple arithmetic operations

Ask the user, for two integer numbers. After that calculate following simple arithmetic operations. Print the summation of these two numbers. Print the subtraction from first number to second number. Print the multiplication of these two numbers. Print the division first number to second number. An example run would be following.

please enter number 1: 10
please enter number 2: 5

summation: 10+5=15
subtraction: 10-5=5
multiplication: 10*5=50
division: 10/5=2

45.5 simple division: Difference between normal and integer division

Ask the user, for two integer numbers. After that calculate following two simple arithmetic operations. Print the normal division (/) first number to second number. Print the integer division (//) first number to second number. An example run would be following.

please enter number 1: 12
please enter number 2: 5

normal division: 12/5=2.4
integer division: 12/5=2

45.6 Input Celsius to Fahrenheit and Fahrenheit to Celsius

Please write a simple Celsius to Fahrenheit and Fahrenheit to Celsius converter. Ask user first which unit they want to convert, C or F. Then ask the value.

Conversion formulas are below.

C = (F - 32) * 5/9


F = (C * 9/5) + 32

An example run:

Which unit you want to convert, C or F: F Value of Fahrenheit: 50

50 Fahrenheit equals to 10 Celsius

45.7 Summation using formula

write a code that find summation of 1..N, where N is an integer given by a user. You should use standard summation formula in the calculation You do not need loop.

Summation formula is below:

45.8 Academic grading in Germany using Modified Bavarian formula

Please write a code which calculates Modified Bavarian formula.

Modified Bavarian formula.

You need to ask minimum possible grade, maximum possible grade and actual grade from the user.

You can see more information about academic grading in Germany.

45.9 Is Number odd or even?

Write a program which asks from user a number and print outs if the entered number is odd or even. An example output could be seen below:

Run 1:

please enter a number: 19
19 is an odd number

Run 2:

please enter a number: 100
100 is an even number

45.10 Question 04: Greatest among 3 numbers

Please write a program which find the greatest number among three numbers. You will ask the user for these three numbers and print greatest of them in the screen.

45.11 Find Lucky Number

Your lucky number is a number between 1 and 9. You can find your lucky number via following procedure:

  1. Ask for birth year: 1987
  2. sum the numbers of birth year: 1 + 9 + 8 + 7 = 25
  3. Since 25 is not a number, you again sum the number of 25: 2 + 5 = 7
  4. Since 7 is a number, you found your lucky number.

Please write a code that ask for birth year and output the lucky number using the above procedure.

45.12 simple error 1

please look at wrong_code1.py. Fix its errors so that it runs correctly.

45.13 simple error 2

please look at wrong_code2.py. Fix its errors so that it runs correctly. There are logical errors on this code.

45.14 simple error 3

please look at wrong_code3.py. Fix its errors so that it runs correctly.

45.15 simple error 4

please look at wrong_code4.py. Fix its errors so that it runs correctly. There are logical errors on this code.

45.16 errors generation nickname

Please look at generation_nickname_errors.py. Fix its errors so that it runs correctly.

45.17 errors generation nickname

Please look at not working exception code. Fix the code so that exceptions are meaningful and error classes obey the hierarchy of python errors.

45.18 Question – find area and circumference of the circle

Write two functions, named find_area and find_circumference which have a single parameter named radius. These functions will calculate area and circumference of the circle using given radius knowledge.

45.19 Question – ordinal suffix writing

Paraphrased from wikipedia

Ordinal numbers may be written in English with numerals and letter suffixes: 1st, 2nd, 3rd, 4th, 11th, 21st, 101st, 477th, etc., with the suffix acting as an ordinal indicator.

Write a function, named get_ordinal_suffix which have a single parameter named num. This function will return a string containing both num and suffix. Rules like below:

- last two digits are 11,12,13 then suffix is "th"
- last digit is 1 then suffix is "st"
- last digit is 2 then suffix is "nd"
- last digit is 3 then suffix is "rd"
- for every other number, suffix is "th"

If your function is correct, below asserts should work without error.

assert get_ordinal_suffix(1) == "1st"
assert get_ordinal_suffix(2) == "2nd"
assert get_ordinal_suffix(3) == "3rd"
assert get_ordinal_suffix(11) == "11th"
assert get_ordinal_suffix(12) == "12th"
assert get_ordinal_suffix(13) == "11th"
assert get_ordinal_suffix(21) == "21st"
assert get_ordinal_suffix(22) == "22nd"
assert get_ordinal_suffix(33) == "33rd"
assert get_ordinal_suffix(101) == "3rd"

assert get_ordinal_suffix(1000) == "1000th"

1000st

45.20 simple random password 1

Create simple program which will print out random password of length 8 in the screen. You do not need loops for simple solution of this question.

following code snippets are useful:

import string
print(string.ascii_letters)
print(string.ascii_letters[20])

Python random module is also necessary.

We can use following code to get random integers:

import random
print(random.randint(3, 9)) 

45.21 Find first digit without string functions

Write a function get_first_digit that finds a first digit of a number without using string functions. You should use only math library or mathematical normal operations. Your function should give true on the following asserts

def get_first_digit(num):
# write your function here

assert get_first_digit(12021) == 1
assert get_first_digit(92021) == 9
assert get_first_digit(72003443) == 7

45.22 Number to English Word

Write a function named number_to_english. This function will accept one integer argument that is between 0 and 10. The function will return English form of number. When you have finished your function, following assert should work.

# write your function here

assert number_to_english(0) == "zero"
assert number_to_english(1) == "one"
assert number_to_english(2) == "two"
assert number_to_english(3) == "three"
assert number_to_english(4) == "four"
assert number_to_english(5) == "five"
assert number_to_english(6) == "six"
assert number_to_english(7) == "seven"
assert number_to_english(8) == "eight"
assert number_to_english(9) == "nine"
assert number_to_english(10) == "ten"

45.23 Question 08: generation nickname

Ask user for their birth year and print out generation nickname.

Generation nickname years
The Greatest Generation born 1901 to 1927
The Silent Generation born 1928 to 1945
Baby Boomers born 1946 to 1954
Generation Jones born 1955 to 1965
Generation X born 1966 to 1980
Millennials born 1981 to 1996
Generation Z born 1997 to 2010
Generation Alpha born after 2011

For example Baby Boomers: Baby boomers were born between 1946 and 1964; therefore, for a birth year of 1960, you will print out.

You have born in 1950. You are a baby boomer.

45.24 (3 5 15) (three five fifteen)

This question is a very common interview question

Write a program which prints the numbers from 1 to 100. For every number, your program should write normal number, but numbers divisible by three it should print out English word three, for numbers divisible by 5, it should print out English word five and for numbers divisible by 15, it should print out fifteen. An example output is below:

1
2
three
4
five
three
7
8
three
five
11
three
13
14
fifteen
16
17

45.25 star triangle 1

Write a simple program, which asks the user for a number N. According to number, which prints out star triangle as below.

An example run:

Please enter a number: 5

*
**
***
****
*****

45.26 star pyramid 1

Write a simple program, which asks the user for an odd number N. According to number, which prints out star pyramid as below.

An example run:

Please enter a number: 5

    *
   ***
  *****
 *******
*********

Do not forget to use string multiplication of python. Run below code and see the output:

print("*" * 1)
print("*" * 2)
print("*" * 3)
print("*" * 4)

45.27 stars rhombus

Please write a python code so that you will see following output, a Rhombus, on the screen. Length for this shape, always odd number, should be given by user. Below rhombus has a length 9.

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

45.28 Is Number odd or even function

Write two functions named is_odd and is_even, both has a single number argument. They should return the true or false according to argument. When you write your functions, below asserts should work. You need to fix following code snippet so that function definitions and assert both work

Assert:

def is_odd
    # write your function here

def is_even
    # write your function here

assert is_odd(19)
assert is_odd(20) == False

assert is_even(19) == False
assert is_even(20)

45.29 function title case

Write a function that give title case for an input. For example given full names below, output will be capitalized as only first char.

if your function works, following asserts should work.

def title_case(word):
    # write code here
    
assert title_case("atilla ozgur") == "Atilla Ozgur"
assert title_case("atiLLa ozgur") == "Atilla Ozgur"
assert title_case("Atilla Ozgur") == "Atilla Ozgur"

assert title_case("ATILLA OZGUR") == "Atilla Ozgur"
assert title_case("AtiLLA OZGur") == "Atilla Ozgur"

45.30 1..N Summation divisible by 3

Write a python code that gets an input number N from user and finds summation 1..N only divisible by 3 numbers. For example, N = 10, then summation is 3+6+9 = 18.

45.31 Project Euler Problem 1

Find the sum of all the multiples of 3 or 5 below 1000. This is the Project Euler Question 1.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

See a mathematical solution here

45.32 Project Euler Problem 2

Solve the Project Euler Question 2

Even Fibonacci numbers Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

45.33 Palindrome 1

Palindrome from wikipedia.

A palindrome is a word, number, phrase, or other sequence of characters 
which reads the same backward as forward, 
such as taco cat or madam or racecar or the number 10801. 

Please write a code that finds if given input is a palindrome. For example: efe, hannah, ava, anna are palindromes. Test your code with above examples and test with at least 3 different non-Palindrome examples. nixon, example, xxxzz For our purposes space and whitespace characters are distinct characters; therefore, “taco cat” is not a palindrome.

Example output:

Please enter a input to test for palindrome: 123
Input text 123 is not a Palindrome.

Please enter a input to test for palindrome: 1221
Input text 1221 is a Palindrome.

Please enter a input to test for palindrome: madam
Input text madam is a Palindrome.

45.34 Question Number Palindrome

Definition of Palindrome from wikipedia.

A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, 
such as taco cat or madam or racecar or the number 10801. 

If we give some palindrome number examples:

  • 121
  • 3443
  • 34543

Some not palindrome numbers

  • 321
  • 4567
  • 123421

Please write a code that finds if given input number is palindrome?

Note Please do not use string library functions.

Example output:

Please enter a number to test: 123
Number 123 is not a Palindrome.
Please enter a number to test: 1221
Number 123 is a Palindrome.

45.35 prime number

Write a simple function that checks if a given number is prime number. It will return true for prime number and false for non-prime number Following asserts then should work.

# write your function here

assert is_prime(20) == False
assert is_prime(19)


# todo add more asserts

45.36 High light a given word in a sentence

Return a given word in a highlighted form in a given sentence. For example, “Jacobs university is a nice place” with nice word should return “Jacobs university is a NICE place”

def highlight_word(sentence, word):
    # write code here


result1=highlight_word("Jacobs university is a nice place","nice")
print(result1) 
# should print Jacobs university is a NICE place"

result2=highlight_word("Programming in python is early in Monday","Monday")
print(result2) 
# should print "Programming in python is early in MONDAY"

45.37 Digit summation of any integer number

entered_number = 190837523478

summation = 1+9+0+8+3+7+5+2+3+4+7+8 = 57

  1. entered_number type int: Solve this question without using any string function only math operators and loop

  2. entered_number type string: solve this question using loop with string indexing

45.38 Find Max and Min in a List of Numbers

You will ask the users a list of numbers that are separated by spaces.

For example:

str_list_of_numbers = input("please enter a list of numbers")

Please write a code that find max and min of these numbers and print them out. String function split should be useful for you.

In [98]: str_list_of_numbers = "1 12 15 21 -3 45 999 0 23"
In [100]: str_list_of_numbers
Out[100]: '1 12 15 21 -3 45 999 0 23'
In [101]: str_list_of_numbers.split(" ")
Out[101]: ['1', '12', '15', '21', '-3', '45', '999', '0', '23']

Example output:

str_list_of_numbers = input("please enter a list of numbers")
1 12 15 21 -3 45 999 0 23
min number: -3
max number: 999

45.39 digit total of 21 but all 3 digits are different

You are asked to find 3-digit numbers which have a digit summation of 21. But all of their digits should be different from each other. For example, 498, 786, 948 fit this definition.

According to this definition, how many different natural numbers can be written?

45.39.1 Solutions

45.40 Question – find sum between indexes

Write following function: You are given following array

from random import sample 
numbers = sample(range(1,101),100)
print(numbers)

and two index, start and stop. Calculate sum of numbers between two indexes.

def find_indices_total(l,start,stop):
    # your code here

45.41 One number in a list is missing, find it

You have a list of number between 1-100 in a list of size 99. One number is missing in this list. How can you find it. Start with following code.

from random import sample 
numbers = sample(range(1,101),99)
print(numbers)

45.41.1 Extra functionality

Generalize above function so that it takes supposed min and max values in the list. For example, it should work with list of numbers with minimum and maximum values as below.

  • 50-100
  • 1000-2000
  • 10-20

Always input list size is the (maximum-minimum-1).

45.42 Input usage with loops and exception handling

Ask user for their name, birthday information. You will then print this information in the screen. You need to use built-in function input for this question. Different then our similar basic question before you need to add error handling to this code when user enters something wrong. If user ask something wrong, you need to ask for same information again. Todo this, you need to add a loop and exception handling to your code. Your code should handle following cases

  • User can not give future date as birth year.
  • User should enter meaningful date.
  • If user enters a text for birthday like two thousand, you need to ask for an number

45.43 Find sum(1..n) > total

get an input from user a number named total. Sum the numbers starting from 1 to n till they are greater than this total. Print out the number

sum(1..n) = new_total > total

Example 1: given total is 20, you stop at 6 since

1 + 2 +3 + 4 +5 = 15 
1 + 2 +3 + 4 +5 +6 = 21

You will print out

sum(1..6) = 21 > 20

Example 2: Think of this question this way. Find me sum(1..n) > 1000. What is n? Here n would be 45.

sum(1..45) = 1035 > 1000

45.44 exception handling simple1

Ask user to enter two numbers and print out multiplication and division of these two numbers. Using exception handling, handle ZeroDivisionError (second number is 0) and ValueError (User enters not a number).

45.45 Palindrome Recursion

Palindrome from wikipedia.

A palindrome is a word, number, phrase, or other sequence of characters 
which reads the same backward as forward, 
such as taco cat or madam or racecar or the number 10801. 

Please write a recursive function that finds if given input is a palindrome. For example: efe, hannah, ava, anna are palindromes. For recursion, you need to work following way:

hannah
anna
nn

Test your function with above examples and test with at least 3 different non-Palindrome examples. nixon, example, xxxzz

45.46 prime number recursion

Write a simple function that checks if a given number is prime number. It will return true for prime number and false for non-prime number Your function should be written in recursive fashion and should use memorization for speed.

Following asserts then should work.

# write your function here

assert is_prime(20) == False
assert is_prime(19)


# todo add more asserts

45.47 Armstrong number

An Armstrong number is a number between 100 and 999. Sum of the cubes of this number digits are equal to itself. For example 371 is an Armstrong number since

3**3 + 7**3 + 1**3 = 371.
  1. Write a function that checks a given number is an Armstrong number

  2. Write a program that uses above function to find all Armstrong numbers

  1. narcissistic numbers

generalization of Armstrong number is the narcissistic numbers

  1. Write a function that checks a given number is a narcissistic number

45.48 Bulgarian solitaire

In the Bulgarian solitaire, you have card list of size 52. Cards in this list have value between 1 and 52. In the start of the game, you need to randomly select 5 cards that total of their card values is equal 45. For example starting cards could be:

10,5,20,7,3 

Write a function which finds a starting 5 cards named

def start_deck(card_list):
    # your code should return a 5 numbers list
    return None

You should make use of random module to create initial list and draw cards from this list. In addition, you should remove already drawn card from list.

45.49 Solutions

45.50 Caesar cipher

  1. Write a python function named encode_in_ceaser_cipher which encodes a given clear text to Ceaser cipher.

  2. Write a python function named decode_from_ceaser_cipher which decodes a given Caesar text to clear text.

Caesar cipher shifts the characters of the given string to right by three for example A becomes D. See below example.

Plain:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher:   DEFGHIJKLMNOPQRSTUVWXYZABC

Do not forget that characters in python string are also numbers. For this question only worry about English Upper case characters.

You will need to use following python functions.

ord
chr

45.51 Create frequency table

extract following compressed file animals.7z in your computer. Read the file using python file functions. And create a following frequency table and print it out.

Dog Cat Bird Fish
AAA BBB CCC DDDD

for AAA..DDD numbers of occurrence of animal should be. Since we have 1000 rows in this csv file, total of these animals should be 1000.

AAA+BBB+CCC+DDD=1000

Easiest way to solve this problem is to use data structure, we learned in this lesson. Hint: it is not list.

45.51.1 Solutions

45.52 Grade school multiplication table.md

Ask user for an number greater than 2, then print out the grade school multiplication table up to number x number. For example below is the output for 3

1   2   3   
2   4   6
3   6   9

45.52.1 Solutions

45.53 Find the largest int value in an int array

Write a function which accepts and int array as an input like below:

def find_max_int(arr):
    pass

This function should find and return the max value in the input array.

45.54 list of digits

Write a python function with one input number. This function returns a list of digits of input number. For example:

input number: 1979 returned value: [1,9,7,9]

45.55 n-th-prime

The first 5 prime numbers: 2, 3, 5, 7, 11 and 6th prime is 13. Write a function which finds nth prime.

def nth_prime(n):
    # return nth prime
    

45.56 Number Guessing game

At the start of the game, create a random number using following code.

import random
random_number = random.randint(1,100)

Ask the use to find this number. You will ask, user to enter a number guessing this input

  • If the entered number is higher than random_number, print out “your guess is too high” and ask user to enter another guess
  • If the entered number is lower than random_number, print out “your guess is too low” and ask user to enter another guess
  • If the entered number is equal to random_number, print out “your guess is right” and finish the program.

45.57 numbers-in-list-find-statistics

You will ask the user to give numbers list as an input. You need to find basic statistics of this numbers. For example given input is like below:

1 12 15 21 -3 45 999 0 23

your output would be as below:

total : 1113
minimum number: -3
maximum number: 999
average: 123

45.58 Pig-Latin

45.59 Pythagoras-doubles.md

find list of two number tuples their squared addition is also squared number.

m*m + n*n = y*y  where m,n < 100

For example (3,4) fit this definition since 33 + 44 = 5*5. What are the other numbers which fit this definition?

45.60 Repeating numbers in list

There are 100 numbers in the list named num_list. Some of these numbers are duplicated in this list. Write a code that finds these repeating numbers.

45.61 sentence-in-a-frame

Write a function that takes two input, sentence and frame character. This function should return a multiline string which surrounds every word in the sentence with a frame character. For example

sentence = “Jacobs University 2019 Class” frame_character = +

Then output would be following

++++++++++++++
+ Jacobs     +
+ University +
+ 2019       +
+ Class      +
++++++++++++++

45.62 simple sudoku checker 1D

Write a simple function which check if a given list obeys the rules of sudoku. In a sudoku puzzle game, a given a list can only contains numbers between 1 to 9. Any given number should occur only once:

Correct:

1, 9, 2, 3, 4, 5, 8, 7, 6 
9, 4, 1, 7, 8, 3, 6, 2, 5

Uncorrect

1, 8, 4, 6, 5, 3, 9, 7, 1

Since 1 occurs twice.

def check_sudoku_correctness(numbers):
    pass

solutions