Skip to content

Commit c181594

Browse files
authored
Add files via upload
1 parent a5b8132 commit c181594

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

ListComprehensions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Let’s say I give you a list saved in a variable:
2+
# a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3+
# Write one line of Python that takes this list a and
4+
# makes a new list that has only the even elements of this list in it.
5+
6+
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
7+
8+
# Here's my line
9+
result = a[1:len(a):2]
10+
# End my line, singular, only one
11+
12+
#I was actually very confused by this prompt, as one could interpret it three ways.
13+
# 1. Even items in list (by testing)
14+
# 2. Even items in this list (odd indeces)
15+
# 3. Items at the even indeces (odd items in this list)
16+
17+
# Okay, so I'm gonna test it with this bit down here, but it doesn't change anything.
18+
# I still only used one line
19+
print(a)
20+
print(result)

ListOverlapComprehensions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random
2+
# Take two lists, say for example these two:
3+
# a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
4+
# b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
5+
# and write a program that returns a list that contains
6+
# only the elements that are common between the lists
7+
# (without duplicates). Make sure your program works on two
8+
# lists of different sizes. Write this using at least one list comprehension.
9+
#Extra:
10+
# Randomly generate two lists to test this
11+
12+
a = random.sample(range(30),random.randint(5,15))
13+
b = random.sample(range(30),random.randint(5,15))
14+
15+
result = [item1 for item1 in a for item2 in b if item1 == item2]
16+
result2 = []
17+
18+
for i in range(len(result)):
19+
isfound = 0
20+
for j in range(i+1, len(result)):
21+
if result[i] == result[j]:
22+
isfound = 1
23+
break
24+
if isfound == 0:
25+
result2.append(result[i])
26+
27+
print(result2)

StringLists.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ask the user for a string and print out whether this string is a palindrome or not.
2+
# (A palindrome is a string that reads the same forwards and backwards.)
3+
4+
string = input("Give me a word to test.\n")
5+
6+
if string[0:int(len(string)/2)].lower() == string[len(string):int((len(string)-1)/2):-1].lower():
7+
print(string + " is a palindrome.")
8+
else:
9+
print(string + " is not a palindrome.")

0 commit comments

Comments
 (0)