Created
February 8, 2025 17:59
-
-
Save rekhailavarasu/cf181623aad61841946f1b1875785990 to your computer and use it in GitHub Desktop.
Lesson 8 homework
This file contains 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
# Challenge 1 | |
# Two Lowest Elements | |
# Your task is to write a program that takes a list of numbers and finds | |
# the two smallest numbers in it. The two smallest numbers can be equal to | |
# each other or different. | |
# | |
# Example: [198, 3, 4, 9, 10, 9, 2], Return: 2, 3 | |
# | |
# Hint: Start with sorting the list | |
#arr = [5, 2, 9, 1, 5, 6] | |
# Your code here | |
def find_smallest_numbers(arr): | |
arr.sort() | |
return arr[0], arr[1] | |
arr = [5, 2, 9, 1, 5, 6] | |
smaller_1, smallest_2 = find_smallest_numbers(arr) | |
print(f'The two smallest numbers are : {smaller_1} and {smallest_2}') | |
# numbers = [30, 22, 94, 12, 15, 66] | |
# smaller_1, smallest_2 = find_smallest_numbers(numbers) | |
# print(f'The two smallest numbers are : {smaller_1} and {smallest_2}') | |
# --------------------------------------------------------------------- | |
# Challenge 2 | |
# Remove Spaces | |
# | |
# Imagine, you're trying to organize your files, and you notice that one | |
# of your file names contains several spaces, which could lead to errors | |
# or difficulties in file management. Your task is to remove all the spaces | |
# from the given file name using a `for` loop. | |
# | |
# Hint: Loop through each character and check if a character is NOT a space. | |
# If it’s not, add the character to the new file name. This way, you will | |
# exclude all spaces. | |
def without_space(file_name): | |
new_file_name = "" | |
for char in file_name: | |
if char != " ": | |
new_file_name += char | |
return new_file_name | |
file_name = "My Summer Photos 2023" | |
new_file_name = without_space(file_name) | |
print(f"Original file name: {file_name}") | |
print(f"New file name without space: {new_file_name}") | |
# --------------------------------------------------------------------- | |
# Challenge 3 | |
# Sum of digits | |
# | |
# Compute the sum of digits in all numbers from 1 to 5. | |
# When the result gets a number n, find the sum of digits | |
# in all numbers from 1 to 5. | |
# | |
# Example: number = 5. Result = 1 + 2 + 3 + 4 + 5 = 15 | |
# Initialize 'number' variable to 5. This is the number up to which we will calculate the sum. | |
#n = 5 | |
# Initialize 'result' variable to 0. This variable will hold the sum. | |
# Iterate through the range starting from 1 up to 'number + 1'. | |
# Add the current value of 'i' to 'result' | |
# Print the sum of integers from 1 to 'number' | |
def sum_number(n): | |
return sum(int(digit) for digit in str(n)) | |
def sum_from_1_to_5(): | |
total_sum = sum(sum_number(i) | |
for i in range(1, 6)) | |
# while total_sum >= 10: | |
# total_sum = sum_number(total_sum) | |
return total_sum | |
number = sum_from_1_to_5() | |
print(number) | |
#-------------------------------------- | |
# def sum_number(n): | |
# return sum(int(digit) for digit in str(n)) | |
# | |
# def sum_from_1_to_n(n): | |
# total_sum = sum(range(1, n + 1)) | |
# return total_sum | |
# | |
# n = int(input("Enter any number: ")) | |
# | |
# number = sum_from_1_to_n(n) | |
# print(number) | |
# --------------------------------------------------------------------- | |
# Challenge 4 | |
# Isogram | |
# | |
# Write a Python program that checks if a given word is an isogram | |
# — an isogram is a word with no repeating letters, such as ‘cat’, ‘taco’, or ‘dog’. | |
# | |
# Example: | |
# For "cat", print True | |
# For "mom", print False | |
# | |
# Choose any word, you’d like to check. | |
# If it’s an isogram, print True. If it’s not an isogram, print False. | |
# | |
# Hints: | |
# Use a for loop to iterate over every letter and conditional statements to check the condition | |
# Use the count() method (think how it can help you solve this problem!) | |
def word_is_isogram(word): | |
for char in word: | |
if word.count(char) > 1: | |
return False | |
else: | |
return True | |
word = str(input("Enter a word: ")) | |
if word_is_isogram(word): | |
print(f"{word} is not isogram") | |
else: | |
print(f"{word} is isogram") | |
# --------------------------------------------------------------------- | |
# Challenge 5 | |
# Repeat digits | |
# | |
# Given a string “312”, your task is to create a new string where each | |
# digit from the original string is repeated a number of times equal to its value. | |
# | |
# | |
# For the input string "312," your program should output "333122." | |
# | |
# Hints: | |
# To make a digit appear as many times as its value (like turning “3” into "333"), | |
# you have to multiply the string by a number. | |
# In Python, multiplying a string by a number repeats the string that many times. | |
# Since our digit is a string, we need to convert it to a number using int() so we can repeat it correctly. | |
# Once the digit is a number, just multiply the character by this value to get the repeated string. | |
string = "312" | |
# Initialize an empty string called 'result' to store the result | |
# Loop through each character in the string | |
# Inside the loop, turn the character into a number using int() | |
# and store it as a current_num variable | |
# Multiply the character by its number value | |
# and add the repeated character to 'result' | |
# Print the final result | |
result = "" | |
for char in string: | |
current_num = int(char) | |
result += char * current_num | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment