0% found this document useful (0 votes)
28 views5 pages

Python Assignment Answers

The document contains multiple Python programs, including one for calculating a four-digit PIN code by finding the smallest digits from three input numbers, and another for converting total seconds into days, hours, minutes, and seconds. It also includes a program to swap two specified letters in a string. Flowcharts and example outputs are provided for each program.

Uploaded by

miralous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Python Assignment Answers

The document contains multiple Python programs, including one for calculating a four-digit PIN code by finding the smallest digits from three input numbers, and another for converting total seconds into days, hours, minutes, and seconds. It also includes a program to swap two specified letters in a string. Flowcharts and example outputs are provided for each program.

Uploaded by

miralous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) Program for calculating PIN codes

This Python program calculates a four-digit PIN code based on a specific logic: it
finds the smallest digit in three given numbers and concatenates them to form the
PIN.

Flowchart (Use appropriate symbols to represent below graph TD)


mermaid
graph TD
A[Start] --> B(Input three numbers: num1, num2, num3);
B --> C{Convert numbers to strings};
C --> D{Initialize empty PIN string};
D --> E{Find minimum digit from num1};
E --> F{Find minimum digit from num2};
F --> G{Find minimum digit from num3};
G --> H{Concatenate smallest digits to form PIN};
H --> I(Print the final PIN);
I --> J[End];

Python code
# Function to find the smallest digit in a number
def find_smallest_digit(number):
# Convert the number to a string to iterate through its digits
s_number = str(number)
# The smallest digit is initialized with the first digit of the number
smallest_digit = int(s_number[0])
# Iterate through the remaining digits of the number
for digit in s_number[1:]:
# If the current digit is smaller than the smallest digit found so
far, update the smallest digit
if int(digit) < smallest_digit:
smallest_digit = int(digit)
# Return the smallest digit found
return smallest_digit

# Take three numbers as input from the user


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))

# Find the smallest digit for each number


smallest_digit1 = find_smallest_digit(num1)
smallest_digit2 = find_smallest_digit(num2)
smallest_digit3 = find_smallest_digit(num3)

# Construct the PIN by concatenating the smallest digits


pin_code = str(smallest_digit1) + str(smallest_digit2) +
str(smallest_digit3)

# Print the generated PIN code


print("The calculated PIN code is:", pin_code)

Output
Enter the first number: 123
Enter the second number: 456
Enter the third number: 789
The calculated PIN code is: 147

1) Python Program to Convert Seconds to Days, Hours, Minutes,


and Seconds
Flowchart to be represented with symbols for graph TD

A[Start] --> B(Input Total Seconds);


B --> C{Calculate Days};
C --> D(days = total_seconds // (24 * 3600));
D --> E(remaining_seconds = total_seconds % (24 * 3600));
E --> F{Calculate Hours};
F --> G(hours = remaining_seconds // 3600);
G --> H(remaining_seconds = remaining_seconds % 3600);
H --> I{Calculate Minutes};
I --> J(minutes = remaining_seconds // 60);
J --> K(seconds = remaining_seconds % 60);
K --> L(Print Result: days:hours:minutes:seconds);
L --> M[End];

Python code
def convert_seconds_to_dhms(total_seconds):
"""
Converts a given number of seconds into days, hours, minutes, and
seconds.
"""
days = total_seconds // (24 * 3600)
remaining_seconds = total_seconds % (24 * 3600)

hours = remaining_seconds // 3600


remaining_seconds %= 3600

minutes = remaining_seconds // 60
seconds = remaining_seconds % 60
return days, hours, minutes, seconds

# Get input from the user


try:
input_seconds = int(input("Enter total seconds: "))
if input_seconds < 0:
print("Please enter a non-negative number of seconds.")
else:
d, h, m, s = convert_seconds_to_dhms(input_seconds)
print(f"Output: {d} days, {h} hours, {m} minutes, {s} seconds")
except ValueError:
print("Invalid input. Please enter an integer.")

Output
Enter total seconds: 1234567
Output: 14 days, 6 hours, 56 minutes, 7 seconds

1) to Convert Seconds to Days, Hours,


Altrenative code for
Minutes, and Seconds

def ConvertHours(n):

minutes = n * 60;
seconds = n * 3600;

print("Minutes = ", minutes \


,", Seconds = " , seconds);

# Driver code
n = 5;
ConvertHours(n);

# This code contributed by Rajput-Ji

Output:

Minutes = 300, Seconds = 18000

3) Python program to swap two letters in a string


This Python program swaps two specified letters within a string. The program first
converts the immutable string into a mutable list of characters. It then uses list
indexing to perform the swap before joining the list back into a new string.

program
def swap_letters(input_string, letter1, letter2):
"""
Swaps all occurrences of two specified letters in a string.

Args:
input_string (str): The original string.
letter1 (str): The first letter to swap.
letter2 (str): The second letter to swap.

Returns:
str: The new string with the letters swapped.
"""
if len(letter1) != 1 or len(letter2) != 1:
print("Error: Please provide single letters for swapping.")
return input_string

char_list = list(input_string)
for i in range(len(char_list)):
if char_list[i] == letter1:
char_list[i] = letter2
elif char_list[i] == letter2:
char_list[i] = letter1

return "".join(char_list)

# --- Example Usage ---


# Get user input
original_word = input("Enter a word: ")
first_letter = input("Enter the first letter to swap: ")
second_letter = input("Enter the second letter to swap: ")

# Perform the swap


exchanged_word = swap_letters(original_word, first_letter, second_letter)

# Print the results


print("\nOriginal string:", original_word)
print("Exchanged string:", exchanged_word)

Flowchart for swapping two letters draw by self using symbols for
below
A[Start] --> B(Input: input_string, letter1, letter2)
B --> C{Is letter1 or letter2 a single character?}
C -- No --> D(Return original string)
C -- Yes --> E(Convert input_string to a list: char_list)
E --> F{For each character in char_list}
F --> G{Is character == letter1?}
G -- Yes --> H(Replace character with letter2)
G -- No --> I{Is character == letter2?}
I -- Yes --> J(Replace character with letter1)
I -- No --> K(Continue to next character)
H --> F
J --> F
K --> F
F --> L(End For loop)
L --> M(Join char_list back into a new string)
M --> N(Return new string)
N --> O(End)

Output
Enter a word: banana
Enter the first letter to swap: a
Enter the second letter to swap: n

Original string: banana


Exchanged string: ababab

You might also like