Created
July 29, 2024 20:31
-
-
Save rafayama/0211a8a526662d237814745187ec829e to your computer and use it in GitHub Desktop.
lesson 5 homework py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Homework Lesson 5 - Workshop - Homework | |
# READ CAREFULLY THE EXERCISE DESCRIPTION AND SOLVE IT RIGHT AFTER IT | |
# Challenge 1 | |
# Make a number Positive | |
# | |
# Create a variable called my_number and set it to any integer value. | |
# Write code to make the number positive if it's negative, and keep it | |
# as is if it's already positive or zero. | |
# | |
# Example: | |
# Input: -3 => Output: 3 | |
# Input: 5 => Output: 5 | |
number = int(input("type a number: ")) | |
num_str = str(number) | |
if number < 0: | |
print(num_str[1::]) | |
else: | |
print(number) | |
# --------------------------------------------------------------------- | |
# Challenge 2 | |
# BinGo! | |
# | |
# If the number is divisible of 3, print “Bin” | |
# If the number is divisible of 7, print “Go” | |
# For numbers which are divisible of 3 and 7, print “BinGo” | |
# Otherwise, print the original number: “{number} is just a number” | |
num = int(input("Type a number: ")) | |
num_str = str(num) | |
if num % 3 == 0 and num % 7 == 0: | |
print("BinGo") | |
elif num % 3 == 0: | |
print("Bin") | |
elif num % 7 == 0: | |
print("Go") | |
else: | |
print(f"{num_str} is just a number") | |
# --------------------------------------------------------------------- | |
# Challenge 3 | |
# Find the middle number | |
# | |
# Given three different numbers x, y, and z, find the number that is neither | |
# the smallest nor the largest and print it. | |
# | |
# Example: | |
# x = 1, y = 5, z = 3 => Output: 3 | |
x = 1 | |
y = 5 | |
z = 3 | |
if x < y < z: | |
print(y) | |
elif x < z < y: | |
print(z) | |
elif y < x < z: | |
print(x) | |
elif y < z < x: | |
print(z) | |
elif z < x < y: | |
print(x) | |
elif z < y < x: | |
print(y) | |
# --------------------------------------------------------------------- | |
# Challenge 4 | |
# Palindrome Numbers | |
# | |
# Ask a user to input a number. | |
# Write a program to check if the given number is a palindrome. | |
# It should print True if the number is a palindrome and False if it is not. | |
# | |
# Palindrome number: 121, 898 | |
num = input("Write a number: ") | |
if num[::-1] == num: | |
print("True") | |
else: | |
print("False") | |
# --------------------------------------------------------------------- | |
# Challenge 5 | |
# Reverse a string | |
# | |
# You're part of a team working on analyzing customer reviews for a new video game. | |
# Due to a software glitch, some reviews have been recorded in reverse with punctuation | |
# at the beginning instead of the end. Your task is to correct these reviews so that they | |
# are in the correct order and the punctuation is appropriately placed at the end of the | |
# sentence or word. | |
# | |
# Example: "tcefreP!" -> Perfect! | |
txt = "tcefreP!" | |
rev_txt = txt[::-1] | |
punctuation = rev_txt[0:1] | |
no_punc_txt = rev_txt[1:] | |
final_txt = no_punc_txt + punctuation | |
print(final_txt) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment