Skip to content

Instantly share code, notes, and snippets.

@ehedaoo
ehedaoo / List1f.py
Created May 2, 2017 09:47
Write a Python program to get the smallest number from a list.
# Write a Python program to get the smallest number from a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
a = [1, 1, 2, 3, 5, 8, 0, 13, 21, 34, 55, 89]
print(min(a))
@ehedaoo
ehedaoo / List1g.py
Created May 2, 2017 10:28
Write a Python program to get the smallest number from a list. Using Functions
# Write a Python program to get the smallest number from a list.
# Using Functions
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
def min_item(a):
flag = a[0]
for i in a:
if i < flag:
flag = i
return flag
@ehedaoo
ehedaoo / List1h.py
Created May 2, 2017 10:59
Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.
# Write a Python program to count the number of strings
# where the string length is 2 or more
# and the first and last character are same from a given list of strings.
# Sample List : ['abc', 'xyz', 'aba', '1221']
def compare(a):
ctr = 0
for i in a:
if len(i) > 2 and i[0] == i[-1]:
@ehedaoo
ehedaoo / List1d.py
Created May 2, 2017 11:09
Write a Python program to get the largest number from a list.
# Write a Python program to get the largest number from a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
print(max(a))
@ehedaoo
ehedaoo / List1a.py
Created May 2, 2017 11:10
Write a Python program to sum all the items in a list.
# Write a Python program to sum all the items in a list.
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
# simple method is to call sum on list itself
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
print(sum(a))
@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)]
@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 / 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 / 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 / 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")