Skip to content

Instantly share code, notes, and snippets.

@ilealm
Last active May 1, 2020 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilealm/6ef45b6327e09060e9a926acb3ea9b45 to your computer and use it in GitHub Desktop.
Save ilealm/6ef45b6327e09060e9a926acb3ea9b45 to your computer and use it in GitHub Desktop.

https://repl.it/student/classrooms/17929

1.3. Input/print: Hello, Harry!

Statement Write a program that greets the user by printing the word "Hello", a comma, the name of the user and an exclamation mark after it. See the examples below.

Warning. Your program's output should strictly match the desired one, character by character. There shouldn't be any space between the name and the exclamation mark. You can use + operator to concatenate two strings. See the lesson for details.

Example input Harry

Example output Hello, Harry!

Read a string:

a = input()

Print a string:

print('Hello, ' + a + '!')


1.4. Input/print: Previous and next

Statement Write a program that reads an integer number and prints its previous and next numbers. See the example below.

Example input 179

Example output The next number for the number 179 is 180 The previous number for the number 179 is 178

Read an integer:

a = int(input())

Print a value:

print('The next number for the number ' + str(a) + ' is ' + str(a + 1)) print('The previous number for the number ' + str(a) + ' is ' + str(a - 1))


1.5. Input/print: Apple sharing

Statement N students take K apples and distribute them among each other evenly. The remaining (the indivisible) part remains in the basket. How many apples will each single student get? How many apples will remain in the basket?

The program reads the numbers N and K. It should print the two answers for the questions above.

Example input 6 50

Example output 8 2

Read the numbers like this:

n = int(input()) k = int(input())

Print the result with print()

print(k // n) print(k % n)


1.6. Input/print: Hours and minutes

Statement Given the integer N - the number of seconds that is passed since midnight - how many full hours and full minutes are passed since midnight?

The program should print two numbers: the number of hours (between 0 and 23) and the number of minutes (between 0 and 1339).

For example, if N = 3900, then 3900 seconds have passed since midnight - i.e. now it's 1:05am. So the program should print 1 65 - 1 full hour is passed since midnight, 65 full minutes passed since midnight.

Example input 3900

Example output 1 65

Read an integer:

a = int(input()) sec_on_hour = 3600

Print a value:

print((a // 3600 ), (a // 60))


1.7. Input/print: Two timestamps

Statement Given two timestamps of the same day: a number of hours, minutes and seconds for both of the timestamps. The moment of the first timestamp happened before the moment of the second one. Calculate how many seconds passed between them.

Example input #1 1 1 1 2 2 2

Example output #1 3661

Example input #2 1 2 30 1 3 20

Example output #2 50

Read an integer:

hour1 = int(input()) minute1 = int(input()) second1 = int(input()) hour2 = int(input()) minute2 = int(input()) second2 = int(input())

obtain the seconds of day1

seconds_stamp_one = ((hour1 * 3600) + (minute1 * 60) + second1) seconds_stamp_two = ((hour2 * 3600) + (minute2 * 60) + second2)

Print a value:

print(seconds_stamp_two - seconds_stamp_one)


2.1. Numbers: Two digits

Statement Given a two-digit integer, print its left digit (a tens digit) and then its right digit (a ones digit). Use the operator of integer division for obtaining the tens digit and the operator of taking remainder for obtaining the ones digit.

Example input 79

Example output 7 9

Read an integer:

a = int(input())

Print a value:

print(a // 10, a % 10)

2.2. Numbers: Swap digits

Statement Given a two-digit integer, swap its digits and print the result.

Example input 79

Example output 97

Read an integer:

a = int(input())

Print a value:

print(str(a % 10)+ str(a // 10))


2.3. Numbers: Last two digits

Statement Given an integer greater than 9, print its last two digits.

Example input 1234

Example output 34

Read an integer:

a = int(input())

Print a value:

print(a % 100)


2.4. Numbers: Tens digit

Statement Given an integer, print its tens digit.

Example input 1234

Example output 3

Read an integer:

a = int(input())

Print a value:

print((a % 100) // 10 )


2.5. Numbers: Sum of digits

Statement Given a three-digit number. Find the sum of its digits.

Example input 123

Example output 6

Read an integer:

a = int(input())

Print a value:

use % to get the number on each positio, and then // to get only the 1st number to the left

print(((a % 1000) // 100) , ((a % 100) // 10), (a % 10))

print(((a % 1000) // 100) + ((a % 100) // 10) + (a % 10))


2.6. Numbers: Digit after decimal point

(this was hard) Statement Given a positive real number, print its first digit to the right of the decimal point.

Example input 1.79

Example output 7

Read a float:

a = float(input())

Print a value:

obtain the integer

print( (a // 1 ) )

obtain the decimal, and then conver to integer by * by 100

print( (a % 1))

obtain the first number to the right of the decimal

print( int((a % 1)*10) // 1)


2.7. Numbers: Car route

Statement A car can cover distance of N kilometers per day. How many days will it take to cover a route of length M kilometers? The program gets two numbers: N and M.

Example input 700 750

Example output 2

import math

Read an integer:

n = int(input()) m = int(input()) print( math.ceil(m / n))


2.8. Numbers: Century

Statement Given a year (as a positive integer), find the respective number of the century. Note that, for example, 20th century began with the year 1901.

Example input 2000

Example output 20

Divide the year by 100, drop the decimals and add one.

year = (int(input()))

if ((year % 100) == 0) : print(year // 100) else: print((year // 100) + 1)


2.B. Numbers: Digital clock

Statement Given the integer N - the number of minutes that is passed since midnight - how many hours and minutes are displayed on the 24h digital clock?

The program should print two numbers: the number of hours (between 0 and 23) and the number of minutes (between 0 and 59).

For example, if N = 150, then 150 minutes have passed since midnight - i.e. now is 2:30 am. So the program should print 2 30.

n_minutes = int(input())

hours = (n_minutes // 60) min = (n_minutes % 60)

print(hours, min)


2.D. Numbers: School desks

Statement A school decided to replace the desks in three classrooms. Each desk sits two students. Given the number of students in each class, print the smallest possible number of desks that can be purchased.

The program should read three integers: the number of students in each of the three classes, a, b and c respectively.

In the first test there are three groups. The first group has 20 students and thus needs 10 desks. The second group has 21 students, so they can get by with no fewer than 11 desks. 11 desks is also enough for the third group of 22 students. So we need 32 desks in total.

Example input 20 21 22

Example output 32

Read an integer:

students_a = int(input()) students_b = int(input()) students_c = int(input())

desk_a = (students_a // 2) if (students_a % 2 == 0) else ( (students_a // 2) + 1 ) desk_b = (students_b // 2) if (students_b % 2 == 0) else ( (students_b // 2) + 1 ) desk_c = (students_c // 2) if (students_c % 2 == 0) else ( (students_c // 2) + 1 )

print(desk_a + desk_b + desk_c)


3.6. If/else: Ascending digits

Statement Given a three-digit integer X consisting of three different digits, print "YES" if its three digits are going in an ascending order from left to right and print "NO" otherwise.

Example input #1 179

Example output #1 YES

Example input #2 197

Example output #2 NO

number = int(input())

#obtain each number hundreds = int(number / 100) tens =int((number - (hundreds * 100)) / 10) units = int(number - (((hundreds * 100) + (tens * 10)) ))

print('hundreds',hundreds, 'tens', tens, 'units', units)

if (hundreds < tens) and (tens < units): print('YES') else: print('NO')

__

3.7. If/else: Four-digit palindrome

Statement Let's call an integer a palindrome if it remains the same after reading its digits from right to left. Given a four-digit integer, print "YES" if it's a palindrome and print "NO" otherwise.

Example input #1 1221

Example output #1 YES

Example input #2 1234

Example output #2 NO

number = int(input())

#obtain each number thousands = int(number / 1000) new_number = (number - (thousands * 1000)) hundreds = int((new_number) / 100) new_number = new_number - (hundreds*100) tens = int((new_number) / 10) new_number = new_number - (tens * 10) units = new_number

print('thousands', thousands, 'hundreds',hundreds, 'tens', tens, 'units', units)

if (thousands == units) and (hundreds == tens): print('YES') else: print('NO')


3.A. If/else: Equal numbers

Statement Given three integers. Determine how many of them are equal to each other. The program must print one of the numbers: 3 (if all are same), 2 (if two of them are equal to each other and the third one is different) or 0 (if all numbers are different).

Example input 10 5 10

Example output 2

value_a = int(input()) value_b = int(input()) value_c = int(input())

equal = 1 if ((value_a == value_b) or (value_a == value_c)) else 0 equal += 1 if ((value_b == value_a) or (value_b == value_c)) else 0 equal += 1 if ((value_c == value_a) or (value_c == value_b)) else 0

print(equal)


3.B. If/else: Order of outlier

Statement Given three integers, in which two are equal to each other and the third one is different. Print the order number of this different one - 1, 2 or 3.

Example input #1 10 5 10

Example output #1 2

Example input #2 10 10 5

Example output #2 3

input_a = int(input()) input_b = int(input()) input_c = int(input())

if (input_b == input_c) : order = 1 if (input_a == input_c) : order = 2 if (input_a == input_b) : order = 3

print(order)


3.C. If/else: Rook move

Chess rook moves horizontally or vertically. Given two different squares of the chessboard, determine whether a rook can go from the first square to the second one in a single move.

The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a rook can go from the first square to the second one in a single move or NO otherwise.

x = int(input()) y = int(input()) intended_x = int(input()) intended_y = int(input())

possible_x_left = x - 1 if (x > 1) else x possible_x_right = x + 1 if (x < 8) else x

possible_y_top = y - 1 if (y > 1) else y possible_y_bottom = y + 1 if (y < 8) else y

print('possible_x_left:', possible_x_left, 'x:', x, 'possible_x_right:', possible_x_right)

print('possible_y_top:', possible_y_top, 'y:', y, 'possible_y_bottom:', possible_y_bottom)

#possibles combinations

print('Posible combinations')

print('move to the left:', 'x:', possible_x_left, 'y:', y)

print('move to the right:', 'x:', possible_x_right, 'y:', y)

print('move to the top:', 'x:', x, 'y:', possible_y_top)

print('move to the bottom:', 'x:', x, 'y:', possible_y_bottom)

can_move='NO' if (possible_x_left >= intended_x) and (intended_y == y) : can_move = 'YES' if (possible_x_right >= intended_x) and (intended_y == y) : can_move = 'YES' if (intended_x == x) and (intended_y >= possible_y_top) : can_move = 'YES' if (intended_x == x) and (intended_y >= possible_y_bottom) : can_move = 'YES'

move on x only

if (intended_y == y) : can_move = 'YES'

move on y only

if (intended_x == x) : can_move = 'YES'

print(can_move)


3.D. If/else: Chessboard - black square

Statement Given a square of a chessboard. If it's a black square, print YES, otherwise print NO.

The program receives two integers from 1 to 8 specifying the column and row number of the square.

column = int(input()) row = int(input())

columnEven = True if (column % 2 == 0) else False rowEven = True if (row % 2 == 0) else False

print('columnEven', columnEven)

print('rowEven', rowEven)

result = 'YES' if (columnEven == rowEven) else 'NO'

print(result)


3.E. If/else: Chessboard - same color

Statement Given two squares of a chessboard. If they are painted in the same color, print YES, otherwise print NO.

The program receives four integers from 1 to 8, each specifying the column and row number, first two - for the first square, and then the last two - for the second square.

columnOne = int(input()) rowOne = int(input()) columnTwo = int(input()) rowTwo = int(input())

def isEven(position): return True if (position % 2 == 0) else False

columnOneEven = isEven(columnOne) rowOneEven = isEven(rowOne) columnTwoEven = isEven(columnTwo) rowTwoEven = isEven(rowTwo)

print('columnOneEven', columnOneEven)

print('rowOneEven', rowOneEven)

resultOne = 'YES' if (columnOneEven == rowOneEven) else 'NO' resultTwo = 'YES' if (columnTwoEven == rowTwoEven) else 'NO'

finalResult = 'YES' if (resultOne == resultTwo) else 'NO'

print(finalResult)


3.F. If/else: King move

Chess king moves one square in any direction. Given two different squares of the chessboard, determine whether a king can go from the first square to the second one in a single move.

The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a king can go from the first square to the second one in a single move or NO otherwise.

currentCol = int(input()) currentRow = int(input()) intendedCol = int(input()) intendedRow = int(input())

canMove = 'NO'

i can move if

move up or down in same column

if ( (intendedCol == currentCol) and ((intendedRow == currentRow + 1) or (intendedRow == currentRow - 1)) ): canMove = 'YES'

# move right or left, same Row

if ((intendedRow == currentRow) and ((intendedCol == currentCol - 1) or (intendedCol == currentRow + 1))): canMove = 'YES'

# move topRight

todo: validate when current is 8. add temp var

if currentRow = 8, then currentRow = 7

if ((intendedCol == currentCol - 1) and (intendedRow == currentRow + 1)) : canMove = 'YES'

# move topLeft

if ((intendedCol == currentCol + 1) and (intendedRow == currentRow + 1)): canMove = 'YES'

# move bottomRight

if ((intendedCol == currentCol - 1) and (intendedRow == currentRow - 1)): canMove = 'YES'

move bottomLeft

if ((intendedCol == currentCol + 1) and (intendedRow == currentRow - 1)): canMove = 'YES'

print('Moving from', currentCol, ',', currentRow, 'to', intendedCol, ',', intendedRow)

print(canMove)


3.G. If/else: Bishop move

Chess bishop moves diagonally in any number of squares. Given two different squares of the chessboard, determine whether a bishop can go from the first square to the second one in a single move.

The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a bishop can go from the first square to the second one in a single move or NO otherwise.

currentCol = int(input()) currentRow = int(input()) intendedCol = int(input()) intendedRow = int(input())

canMove = 'NO'

move diagonal left up

if ((intendedCol < currentCol) and (intendedRow > currentRow)) : canMove = 'YES'

move diagonal right up

if ((intendedCol > currentCol) and (intendedRow > currentRow)): canMove = 'YES'

move diagonal bottom Left

if ((intendedCol < currentCol) and (intendedRow < currentRow)): canMove = 'YES'

move diagonal bottom Right

if ((intendedCol > currentCol) and (intendedRow < currentRow)): canMove = 'YES'

print('Moving from', currentCol, ',', currentRow, 'to', intendedCol, ',', intendedRow)

print(canMove)


3.I. If/else: Knight move

Chess knight can move to a square that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. The complete move therefore looks like the letter L. Given two different squares of the chessboard, determine whether a knight can go from the first square to the second one in a single move.

The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a knight can go from the first square to the second one in a single move or NO otherwise.

currentCol = int(input()) currentRow = int(input()) intendedCol = int(input()) intendedRow = int(input())

canMove = 'NO'

move to the right

if ( (intendedCol == currentCol + 2 ) and ( (intendedRow == currentRow + 1) or (intendedRow == currentRow - 1) ) ) : canMove = 'YES'

move to the left

if ( (intendedCol == currentCol - 2 ) and ( (intendedRow == currentRow + 1) or (intendedRow == currentRow - 1) ) ) : canMove = 'YES'

move to the top

if ( (intendedRow == currentRow + 2 ) and ( (intendedCol == currentCol + 1) or (intendedCol== currentCol - 1) ) ) : canMove = 'YES'

move to the bottom

if ( (intendedRow == currentRow - 2 ) and ( (intendedCol == currentCol + 1) or (intendedCol== currentCol - 1) ) ) : canMove = 'YES'

print(canMove)


3.J. If/else: Leap year

Statement Given the year number. You need to check if this year is a leap year. If it is, print LEAP, otherwise print COMMON.

The rules in Gregorian calendar are as follows:

a year is a leap year if its number is exactly divisible by 4 and is not exactly divisible by 100 a year is always a leap year if its number is exactly divisible by 400

Warning. The words LEAP and COMMON should be printed all caps.

year = int(input())

isLeap = 'COMMON'

if ( ((year % 4 == 0) and (year % 100 > 0)) or (year % 400 == 0) ) : isLeap = 'LEAP'

print(isLeap)


3.L. If/else: Linear equation

Write a program that solves a linear equation ax = b in integers. Given two integers a and b (a may be zero), print a single integer root if it exists and print "no solution" or "many solutions" otherwise.

Example input #1 1 -2

Example output #1 -2

Example input #2 2 -1

Example output #2 no solution

a = int(input()) b = int(input())

if a != 0: x = b / a result = int(x) if x == int(x) else 'no solution' if x == 0 : result = 'no solution' else: result = 'no solution'

if (b==0): if (a == 0) : result = 'many solutions' else: result = 0

print(result)

3.N. If/else: Vertices of rectangle

Statement Given integer coordinates of three vertices of a rectangle whose sides are parallel to coordinate axes, find the coordinates of the fourth vertex of the rectangle.

Example input #1 1 5 7 5 1 10 three vertices are (1, 5), (7, 5), (1, 10)

Example output #1 7 10

Example input #2 1 5 7 10 1 10

Example output #2 7 5

xA = int(input()) yA = int(input()) xB = int(input()) yB = int(input()) xC = int(input()) yC = int(input())

print('A', xA, yA)

print('B', xB, yB)

print('C', xC, yC)

find the x with is not doubled

if (xA == xB): xD = xC else: if (xB == xC): xD = xA else: xD = xB

find the y with is not doubled

if (yA == yB): yD = yC else: if (yB == yC): yD = yA else: yD = yB

print(xD) print(yD)


3.O. If/else: Sort three numbers

Statement Given three integers, print them in ascending order.

Example input 5 3 7

Example output 3 5 7

valueA = int(input()) valueB = int(input()) valueC = int(input())

results = []

results.append(valueA) results.append(valueB) results.append(valueC)

results.sort()

for number in results: print(number)

@ilealm
Copy link
Author

ilealm commented Apr 28, 2020

These are my answers to my first Python tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment