Skip to content

Instantly share code, notes, and snippets.

@ehedaoo
ehedaoo / Practice15a.py
Created May 25, 2017 03:56
Generate Random Password
# Write a password generator in Python.
# strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols.
# The passwords should be random, generating a new password every time the user asks for a new password.
# Include your run-time code in a main method.
import string
import random
characters = string.ascii_letters
@ehedaoo
ehedaoo / Practice11.py
Created May 16, 2017 14:58
Create random list of variable length and print common unique elements from both the lists in single line of code.
# Generate two random lists for different lengths
# and write a program that returns a list that contains only the elements
# that are common between the lists (without duplicates).
# Make sure your program works on two lists of different sizes.
import random
test1 = int(input("Enter the length of the first array: "))
test2 = int(input("Enter the length of the second array: "))
@ehedaoo
ehedaoo / Practice9.py
Created May 16, 2017 08:16
Print Fibonacci Series
# Print Fibonacci Series
a = 0
b = 1
test = int(input("Enter the range till you want the series: "))
print(a)
print(b)
for i in range(test):
@ehedaoo
ehedaoo / Practice10.py
Created May 16, 2017 07:39
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right.
# Generate a random number between 1 and 9 (including 1 and 9).
# Ask the user to guess the number,
# then tell them whether they guessed too low, too high, or exactly right.
import random
num = random.randint(1, 10)
flag = 0
@ehedaoo
ehedaoo / Practice7.py
Created May 11, 2017 05:01
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
# a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100].
# Write one line of Python that takes this list a
# and makes a new list that has only the even elements of this list in it.
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b = list(filter(lambda x: x % 2 == 0, a))
print(b)
@ehedaoo
ehedaoo / Practice6.py
Created May 9, 2017 20:17
Ask the user for a string and print out whether this string is a palindrome or not.
# Ask the user for a string and print out whether this string is a palindrome or not.
raw = str(input("Enter a string: "))
raw.replace(" ", "")
set1 = str.lower(raw[::-1])
set2 = str.lower(raw[0::])
if set2 == set1:
print("Palindrome")
else:
print("Not a palindrome")
@ehedaoo
ehedaoo / Practice5a.py
Created May 9, 2017 04:45
Take two random lists and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.
# Take two random lists
# and write a program that returns a list that contains only the elements
# that are common between the lists (without duplicates).
# Make sure your program works on two lists of different sizes.
import random
a = random.sample(range(100), 10)
b = random.sample(range(100), 15)
c = []
@ehedaoo
ehedaoo / Practice5.py
Created May 9, 2017 04:44
Take two lists and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.
# Take two lists, say for example these two:
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
# and write a program that returns a list that contains only the elements
# that are common between the lists (without duplicates).
# Make sure your program works on two lists of different sizes.
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = []
@ehedaoo
ehedaoo / Practice4.py
Created May 6, 2017 04:18
Create a program that asks the user for a number and then prints out a list of all the divisors of that number.
# Create a program that asks the user for a number
# and then prints out a list of all the divisors of that number.
num = int(input("Enter a number"))
check = list(range(1, num+5))
for number in check:
if num % number == 0:
print(number)
@ehedaoo
ehedaoo / List1i.py
Created May 4, 2017 04:48
Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples. Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
# Write a Python program to get a list,
# sorted in increasing order by the last element
# in each tuple from a given list of non-empty tuples.
# Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]