[go: nahoru, domu]

Skip to content

Commit

Permalink
continued learning
Browse files Browse the repository at this point in the history
  • Loading branch information
mite404 committed Feb 3, 2024
1 parent 741c5d2 commit 1ccd14a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 010_fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Challenge 10: FizzBuzz
# Write a program that prints the numbers from 1 to 20.
# For multiples of 3, print "Fizz" instead of the number,
# and for multiples of 5, print "Buzz."
#
# For numbers which are multiples of both three and five, print "FizzBuzz."


# Pseudocode workflow example:
'''
For each number from 1 to 20:
If the number is divisible by both 3 and 5:
Print "FizzBuzz"
Else, if the number is divisible by 3:
Print "Fizz"
Else, if the number is divisible by 5:
Print "Buzz"
Else:
Print the number
'''


# defining my own function
def fizzbuzz():
for number in range(1, 21):
if number % 3 == 0 and number % 5 == 0:
print('FizzBuzz')
elif number % 3 == 0:
print('Fizz')
elif number % 5 == 0:
print('Buzz')
else:
print(number)


# ask user for input
while True:
usernum = int(input('Please enter a number (0-20): '))
if 1 <= usernum <= 20:
break
else:
print('Ivalid input. Please enter a number between 0-20.')


# call the function
fizzbuzz()
38 changes: 38 additions & 0 deletions 11_palindrome_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Challenge 1: Palindrome Checker
#
# Write a Python function that checks if a given string is a palindrome.
# A palindrome is a word, phrase, number, or other sequence of characters
# that reads the same forward and backward, ignoring spaces, punctuation,
# and capitalization.
import re


def askforString(): # ask user to enter a string
s = input('Please enter a palindrome: ')
return s


def is_palindrome(s):
# convert the string to lowercase
s = s.lower()

# remove non-alphanumberic chars
s = re.sub(r'\W+', '', s)

# check if the string is a palindrome
reversed_s = s[::-1]

if s == reversed_s:
print(f"{s} is a palindrome!")
else:
print(f"{s} is not a palindrome, please try again.")


def main(): # calling the functions
user_input = askforString()
is_palindrome(user_input)


# calling the main function
if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions 12_factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# The factorial function says to multiply all whole numbers from our
# number down to 1.
#
# Examples: 4! = 4 × 3 × 2 × 1 = 24.

def factorial(n):
result = 1 # Initialize the result variable outside the loop

while n > 1: # Use a condition to control the loop
result *= n # Update the result variable by multiplying with the current value of n
n -= 1 # Decrement n to move towards the base case

return result # Return the final result after the loop

# Test Cases
print(factorial(5))
print(factorial(0))

0 comments on commit 1ccd14a

Please sign in to comment.