Skip to content

Commit 678621a

Browse files
authored
Merge pull request #172 from charan-s108/main
Added URL Shortner & Number Guessing Game to "Coding"
2 parents c5a754f + 8e1840b commit 678621a

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Coding/Number Guessing Game.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Number Guessing Game
2+
# This program generates a random number and asks the player to guess it.
3+
# It provides feedback on whether the guess is too high or too low until the correct number is guessed.
4+
5+
import random
6+
7+
def guess_number():
8+
# Generate a random number between 1 and 100 (inclusive)
9+
secret_number = random.randint(1, 100)
10+
11+
attempts = 10 # Initialize the number of attempts
12+
13+
print("Welcome to the Number Guessing Game!")
14+
print("I'm thinking of a number between 1 and 100.")
15+
16+
while True:
17+
try:
18+
# Ask the player for their guess
19+
guess = int(input("Guess the number: "))
20+
21+
# Increment the number of attempts
22+
attempts += 1
23+
24+
if guess < secret_number:
25+
print("Too low! Try again.")
26+
elif guess > secret_number:
27+
print("Too high! Try again.")
28+
else:
29+
print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts!")
30+
break # Exit the loop if the guess is correct
31+
32+
except ValueError:
33+
print("Invalid input. Please enter a valid number.")
34+
35+
if __name__ == "__main__":
36+
guess_number()

Coding/URL Shortner.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# URL Shortener Program
2+
# This program generates short URLs for long URLs and provides redirection to the original URL.
3+
4+
import hashlib
5+
6+
# Dictionary to store URL mappings (short URL to long URL)
7+
url_mapping = {}
8+
9+
def generate_short_url(long_url):
10+
# Hash the long URL to generate a unique identifier
11+
hash_object = hashlib.md5(long_url.encode())
12+
hash_hex = hash_object.hexdigest()
13+
14+
# Take the first 8 characters of the hash as the short URL
15+
short_url = hash_hex[:8]
16+
17+
return short_url
18+
19+
def shorten_url():
20+
print("Welcome to the URL Shortener!")
21+
long_url = input("Enter the long URL to shorten: ")
22+
23+
# Check if the long URL is already in the mapping
24+
if long_url in url_mapping:
25+
print("Short URL: " + url_mapping[long_url])
26+
else:
27+
# Generate a new short URL and add it to the mapping
28+
short_url = generate_short_url(long_url)
29+
url_mapping[long_url] = short_url
30+
print("Short URL: " + short_url)
31+
32+
def redirect_url():
33+
short_url = input("Enter the short URL to redirect: ")
34+
35+
# Check if the short URL exists in the mapping
36+
if short_url in url_mapping.values():
37+
# Find the corresponding long URL
38+
long_url = next(key for key, value in url_mapping.items() if value == short_url)
39+
print("Redirecting to the original URL: " + long_url)
40+
else:
41+
print("Short URL not found. Please enter a valid short URL.")
42+
43+
if __name__ == "__main__":
44+
while True:
45+
print("\nMenu:")
46+
print("1. Shorten a URL")
47+
print("2. Redirect a URL")
48+
print("3. Exit")
49+
50+
choice = input("Select an option (1/2/3): ")
51+
52+
if choice == "1":
53+
shorten_url()
54+
elif choice == "2":
55+
redirect_url()
56+
elif choice == "3":
57+
break
58+
else:
59+
print("Invalid choice. Please select a valid option.")

0 commit comments

Comments
 (0)