-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
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
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() |
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
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() |
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
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)) |