Skip to main content

Reverse a linked list

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

Shuffle Deck of Cards

0 likes • May 31, 2023 • 1 view
Python
import random
# Define the ranks, suits, and create a deck
ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
deck = [(rank, suit) for rank in ranks for suit in suits]
# Shuffle the deck
random.shuffle(deck)
# Display the shuffled deck
for card in deck:
print(card[0], "of", card[1])

combine values

0 likes • Nov 19, 2022 • 0 views
Python
from collections import defaultdict
def combine_values(*dicts):
res = defaultdict(list)
for d in dicts:
for key in d:
res[key].append(d[key])
return dict(res)
d1 = {'a': 1, 'b': 'foo', 'c': 400}
d2 = {'a': 3, 'b': 200, 'd': 400}
combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}

Input 2D Matrix

0 likes • Nov 19, 2022 • 0 views
Python
# Input for row and column
R = int(input())
C = int(input())
# Using list comprehension for input
matrix = [[int(input()) for x in range (C)] for y in range(R)]

Number guessing game

0 likes • Nov 19, 2022 • 0 views
Python
""" Number Guessing Game
----------------------------------------
"""
import random
attempts_list = []
def show_score():
if len(attempts_list) <= 0:
print("There is currently no high score, it's yours for the taking!")
else:
print("The current high score is {} attempts".format(min(attempts_list)))
def start_game():
random_number = int(random.randint(1, 10))
print("Hello traveler! Welcome to the game of guesses!")
player_name = input("What is your name? ")
wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name))
// Where the show_score function USED to be
attempts = 0
show_score()
while wanna_play.lower() == "yes":
try:
guess = input("Pick a number between 1 and 10 ")
if int(guess) < 1 or int(guess) > 10:
raise ValueError("Please guess a number within the given range")
if int(guess) == random_number:
print("Nice! You got it!")
attempts += 1
attempts_list.append(attempts)
print("It took you {} attempts".format(attempts))
play_again = input("Would you like to play again? (Enter Yes/No) ")
attempts = 0
show_score()
random_number = int(random.randint(1, 10))
if play_again.lower() == "no":
print("That's cool, have a good one!")
break
elif int(guess) > random_number:
print("It's lower")
attempts += 1
elif int(guess) < random_number:
print("It's higher")
attempts += 1
except ValueError as err:
print("Oh no!, that is not a valid value. Try again...")
print("({})".format(err))
else:
print("That's cool, have a good one!")
if __name__ == '__main__':
start_game()

Remove numbers from files

0 likes • Mar 26, 2023 • 0 views
Python
import os
# Get the current directory
current_dir = os.getcwd()
# Loop through each file in the current directory
for filename in os.listdir(current_dir):
# Check if the file name starts with a number followed by a period and a space
if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ':
# Remove the number, period, and space from the file name
new_filename = filename[3:]
# Rename the file
os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))

Size of tuple

0 likes • Nov 19, 2022 • 0 views
Python
import sys
# sample Tuples
Tuple1 = ("A", 1, "B", 2, "C", 3)
Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")
Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))
# print the sizes of sample Tuples
print("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")
print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")
print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")