Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
Binary file added __pycache__/my_dice.cpython-38.pyc
Binary file not shown.
40 changes: 40 additions & 0 deletions hackerrank.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 1. Diagonal Difference: https://www.hackerrank.com/challenges/diagonal-difference/problem
def diagonalDifference(arr):
n=int(input(''))
x1=[arr[i][i]for i in range(n)]
x2=[arr[n-i-1][i]for i in range(n)]
return abs(sum(x1)-sum(x2))

# 2. Left Rotation: https://www.hackerrank.com/challenges/array-left-rotation/problem
def rotateLeft(d, arr):
for i in range(d):
arr.append(arr[0])
arr.pop(0)
return arr

# 3. Counter game: https://www.hackerrank.com/challenges/counter-game/problem
import math
def closest_power_of_2(n):
return 2**math.floor(math.log(n,2))

def counterGame(n):
string=['Louise','Richard']
counter_string=-1
while n!=1:
counter_string+=1 # l
closest=closest_power_of_2(n)
if closest==n:
n=n/2
else:
n=n-closest
return string[counter_string%2]

# 4. Time Delta: https://www.hackerrank.com/challenges/python-time-delta/problem
import datetime

# Complete the time_delta function below.
def time_delta(t1, t2):
ti1=datetime.datetime.strptime(t1,'%a %d %b %Y %H:%M:%S %z')
ti2=datetime.datetime.strptime(t2,'%a %d %b %Y %H:%M:%S %z')
ti3=ti1-ti2
return str(abs(ti3.days*86400+ti3.seconds))
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import my_dice
dice=my_dice.rollDice(int(input("Enter repetition number: ")))
for i in range(len(dice)):
print("The probability of {} is {}%".format(i+1,dice[i]))
9 changes: 9 additions & 0 deletions my_dice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from random import randint
def rollDice(number):
dice=[0]*6 # create a dice list with 6 elements
for i in range(number):#generate 5000 number between 1 and 6
dice[randint(1,6)-1]+=1
dice=[round((dice[i]/number)*100,2) for i in range(len(dice))]
return dice

rollDice(340)
46 changes: 46 additions & 0 deletions q1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ## 1. Password
# Write a Python program to check the validity of a password (input from users)

# **Rules :**
# - At least 1 letter between [a-z] and 1 letter between [A-Z]
# - At least 1 number between [0-9].
# - At least 1 character from [$#@].
# - Minimum length 6 characters.
# - Maximum length 16 characters.
# If password is not valid throw `ValueError` with a proper error message for each rule. If the password is valid print a success message. Use some from `raise`, `except`, `assert`, `else` and `finally` keywords.
#First way
import re
def password_validation(password):
if bool(re.findall("^.*(?=.{6,16}).*$",password))==False:
raise ValueError("This password is not valid. You should use at least 6 characters and max 16 chacters.")
elif bool(re.findall("^.*(?=.*[A-Z]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one upper letter.")
elif bool(re.findall("^.*(?=.*[0-9]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one digit.")
elif bool(re.findall("^.*(?=.*[$#@]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one of them ($,#,@).")
elif bool(re.findall("^.*(?=.*[a-z]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one lower letter.")
else:
return 'Password is valid.'
print(password_validation(input(""))) # aAzc1$22

#second way
import re
def password_validation(password):
if bool(re.findall("^.*(?=.{6,16}).*$",password))==False:
raise ValueError("This password is not valid. You should use at least 6 characters and max 16 chacters.")
elif bool(re.findall("^.*(?=.*[A-Z]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one upper letter.")
elif bool(re.findall("^.*(?=.*[0-9]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one digit.")
elif bool(re.findall("^.*(?=.*[$#@]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one of them ($,#,@).")
elif bool(re.findall("^.*(?=.*[a-z]).*$",password))==False:
raise ValueError("This password is not valid. You should use at least one lower letter.")
else:
return 'Password is valid.'
try:
print(password_validation(input(""))) # aAzc1$22
except ValueError as e:
print(ValueError,e)
13 changes: 13 additions & 0 deletions q2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ## 2. Dice Percentage
# * Create an array with 6 elements named `dice`. Fill this array with the value zero. Generate a random number with a value between 1 and 6 (just like a **dice**) in a repetition 5000 times.

# * If the value is 1, increase the element 0 in the array by 1, the same applies to the values 2, 3, 4, 5 and 6. The dice[0] element indicates the number of times value 1 has occurred. Or in general: dice[x-1] indicates the number of times that x has been thrown.

# * At the end of the repetition, print the contents of the array as percentages with 2 decimal places. For example; `"Percentage of throws of value 3 = 16.28%"`

from random import randint
dice=[0]*6 # create a dice list with 6 elements
for i in range(5000):#generate 5000 number between 1 and 6
dice[randint(1,6)-1]+=1
for i in range(len(dice)):
print('Percentage of throws of value {} = {:.2f}%'.format(i+1,(dice[i]/5000)*100))
22 changes: 22 additions & 0 deletions q4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ## 4. The Greatest Common Divisor
# As a user, I want to use a program which can calculate the `greatest common divisor (GCD)` of my inputs.

# Acceptance Criteria:
# - Ask user the enter the number of inputs (n).
# - Ask user to enter `n` input numbers one by one.
# - Use try/except blocks to verify input entries and warn the user for Nan or non numerical inputs.
# - Calculate the greatest common divisor (GCD) of `n` numbers.
# - Use `gcd` function in module of math.
from math import gcd
n=input("n: ")
liste=[]
try:
n=int(n)
for i in range(n):
liste.append(int(input("value: ")))
g=liste[0]
for i in range(len(liste)):
g=gcd(g,liste[i])
print(g)
except (ValueError) as e:
print('Error Code: ',e)