Classic Computer Science Problems in Python (Part 1)

A Quick Review to Problem Solving

In this Article we will try to solve famous computer science problems in different ways . Firstly we will analyse every problem statement and build a prototype of it, then solve it in different ways to fully understand and have grip on it. This is a part of a long series in this Notebook 1 there are problems related to string manipulation and some famous algorithms are solved in the next Book problem related to Data Structure will counter.

Things you will learn :

• Algorithms
• Problems Solving
• Interview Preparations
• Quick Review of Concepts

Let's get started by simplify a very general problem in computer science

1. Palindromes:

A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as “madam” or “racecar”.

The Challenge

Given a string , return true or false according to whether or not the string is a palindrome.

`check_palindrome(“madam”) ## True`

Building Algorithmic Thinking

• our function will receive a single parameter of string
• to avoid any upper or lower case issue we first convert our string to lower case using str.lower()
• Now we will take the reverse of our string
• finally check if reversed string and the given string are matched
`// function ( single parameter string) // take empty parameter of reverse string// take lowercase of string// reverse str using for loop// return reverse string// function return will be passed in result// check whether string and result are equal or not `
`def check_palindrome(s):    rev=''    str=s.lower()    for i in str:        rev=i+rev    return revs="madam"result=check_palindrome(s)if result==s:    print(f" {s} is in Palindrome")else :    print(f"{s} not in Palindrome")madam is in Palindrome`

2. Fizz Buzz:

The “Fizz-Buzz test” is an interview question designed to help filter out the 99.5% of programming job candidates.FizzBuzz is a very simple programming task that only uses the basic core of loops and mathematics involvement.

The Challenge

In this program we will take an integer input or constant integer value and print Fizz if the number is divisible to 3 or print Buzz if the number in divisible to 5 or else if the number is divisible to both print FizzBuzz.

Building Algorithmic Thinking

• Firstly check the given integer should not be less than 0
• we will utilize the mathematical relationship between divisor , quotient and reminder . https://www.math-only-math.com/dividend-divisor-quotient-and-remainder.html
• if some number is completely divisible to other its reminder will be 0
• using this relationship check mode of 3 & 5
• if number mode of 3 is zero it will be “Fizz”
`// Take input integer n// check if integer n is not less than zero// in for loop that will in range of n+1 (as zero not include)// check iterator i mode with 3 and 5 or both `
`n=input (“Enter any integer number:”)n=int(n)if (n<0): print (“Error , enter positive integer”)for i in range(n+1): if i%3==0 and i%5==0: print (“FizzBuzz”)  continue # continue statement will return control to the begining of loop elif i%5==0: print (“Buzz”) continue elif i%3==0: print (“Fizz”) continue print(i)Enter any integer number:12FizzBuzz12Fizz4BuzzFizz78FizzBuzz11Fizz`

Object Oriented FizzBuzz

`class FizzBuzz(object): def fun(self,n): for i in range(n+1): if i%3==0 and i%5==0: print (“FizzBuzz”)continue # continue statement will return control to the begining of loop elif i%5==0: print (“Buzz”) continue elif i%3==0: print (“Fizz”) continue print(i)fizz1=FizzBuzz()print(fizz1.fun(15))FizzBuzz12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzzNone`

It's just two examples of Notebook you can have many more problems and their solutions available to my GitHub Repository https://github.com/aaliashraf/Classic-Computer-Science-Problems

Conclusion:

It's a review of some classic problems that we done in Fundamental of Programming . It will give a very quick revision of interview questioning problems under one platform . For new to python or shifted from C++ or Java in Classic Problem solving guide you to understand basics core of Python programming. In the next part of Notebook problems related to data structure will be encountered.

A student learning the way of problem solving.

A student learning the way of problem solving.