Skip to content

Commit 7ce64ae

Browse files
committed
Merge pull request #94 from tsegas/master
add slice lab1 project
2 parents 9cd547d + 1a1a287 commit 7ce64ae

File tree

8 files changed

+396
-27
lines changed

8 files changed

+396
-27
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# list lab execises from session-03
2+
3+
import random
4+
import string
5+
import sys
6+
import os
7+
8+
# List of fruits
9+
mylist = ["apples", "pears","oranges","peaches"]
10+
11+
# Display the list
12+
print('\n',"List of fruit",mylist,'\n')
13+
14+
# Prompt for another type of fruit
15+
newfruit = input('Enter a type of fruit:')
16+
17+
mylist.append(newfruit)
18+
19+
mylen = len(mylist)
20+
21+
# Display the list with the new fruit added
22+
print("List of fruit with the {} added".format(newfruit),mylist,'\n')
23+
24+
# Prompt for a number
25+
num = int(input('Enter a number from 1 to {:d}:'.format(mylen)))
26+
#int(input("Please enter an integer: "))
27+
28+
for i in range(0,mylen):
29+
if (i == num-1):
30+
print("The fruit at that number is",mylist[i],'\n')
31+
32+
addfruit = ["lemon"]
33+
mylist = addfruit + mylist
34+
35+
# Display the list with lemon added
36+
print("List of fruit with lemon added",mylist)
37+
38+
mylist.insert(0,"kiwi")
39+
40+
# Display the list with kiwi added
41+
print("List of fruit with kiwi added",mylist,'\n')
42+
43+
for i in mylist:
44+
# find fruits that start with a 'P'
45+
if (i[0] == "p"):
46+
print("This fruit starts with p",i,'\n')
47+
48+
49+
############################## Series 2 ---- actions #######################################
50+
# Display the list
51+
print("List of fruit:",mylist,'\n')
52+
53+
# Remove the last fruit
54+
mylist.pop()
55+
56+
# Display the list
57+
print("List of fruit with the last item removed:",mylist,'\n')
58+
59+
# Double the elements in the list
60+
mylist = mylist + mylist
61+
# Display the list
62+
print("List of fruit.... multiplied by 2:",mylist,'\n')
63+
64+
# Prompt for a fruit to delete
65+
delfruit = input('Enter a fruit to be deleted:')
66+
67+
temp = mylist.copy()
68+
lenth = len(temp)
69+
70+
# for loop to go through the list and delete occurances of the seleted fruit
71+
for i in range(0,(lenth-1)):
72+
if (temp[i] == delfruit):
73+
#delete the spcified fruit
74+
mylist.remove(temp[i])
75+
else:
76+
print("This fruit was not selected for removal",temp[i],'\n')
77+
78+
# Display the list with the selected fruit removed
79+
print("List of fruit with all occurances of {} removed".format(delfruit),mylist,'\n')
80+
81+
############################## Series 3 ---- actions #######################################
82+
83+
# Change each fruit's first letter to capital
84+
ntemp = mylist.copy()
85+
for i, elem in enumerate(ntemp):
86+
s = elem
87+
mylist.remove(elem)
88+
# Set the first letter in each item in the list to upper case
89+
s = s.capitalize()
90+
elem = s
91+
# insert the the capitalized item right back in its place
92+
mylist.insert(i,elem)
93+
94+
# Display the list with each item capitalized
95+
print("List of fruit with each item capitalized ...:",mylist,'\n')
96+
97+
# Change each fruit's first letter to lower case
98+
ntemp = mylist.copy()
99+
for i, elem in enumerate(ntemp):
100+
s = elem
101+
mylist.remove(elem)
102+
# Set the first letter in each item in the list to lower case
103+
s = s.lower()
104+
elem = s
105+
# insert the the capitalized item right back in its place
106+
mylist.insert(i,elem)
107+
108+
# Display the list with each item in lower case
109+
print("List of fruit with each item in lower case ...:",mylist,'\n')
110+
111+
tlist = mylist.copy()
112+
# Loop through the list and ask the user if they like the fruit item
113+
for i in tlist:
114+
# Prompt for a fruit to delete
115+
answ = (input('Do you like {}:'.format(i)))
116+
if (answ == "no"):
117+
# Remove the item
118+
mylist.remove(i)
119+
elif (answ == "yes"):
120+
print('you like this fruit:',i,'\n')
121+
# insist on a "yes" or "no" answer
122+
else:
123+
answ = input('Enter "yes" or "no" Please:')
124+
if (answ == "no"):
125+
# Remove the item
126+
mylist.remove(i)
127+
elif (answ == "yes"):
128+
print('you like this fruit:',i,'\n')
129+
130+
# Display the list of liked fruits
131+
print("List of fruit with the ones you don't like removed:",mylist,'\n')
132+
133+
############################## Series 4 ---- actions #######################################
134+
135+
# copy the last list
136+
nextlist = mylist.copy()
137+
print("List of fruit displayed... ",nextlist,'\n')
138+
nlist = []
139+
ilist = []
140+
# loop through the list of fruit items
141+
for i in nextlist:
142+
n = 1
143+
strg = ''
144+
# Loop through each fruit item to get each letter that needs to be reversed
145+
for j in i:
146+
ln = len(i)
147+
last = (i[ln-n])
148+
n = n+1
149+
if (n > 1):
150+
strg = ''.join((strg,last))
151+
ilist = [strg]
152+
nlist = nlist + ilist
153+
154+
# Display the list of liked fruits
155+
print("List of fruit with letters in each fruit reversed...",nlist,'\n')
156+
157+
# copy the list
158+
lastlist = nextlist.copy()
159+
160+
# Remove last item in the list
161+
lastlist.pop()
162+
163+
# Display the original list
164+
print("The original list of fruit displayed... ",nextlist,'\n')
165+
166+
167+
# Display the original list
168+
print("The original list of fruit with the last item deleted... ",lastlist,'\n')
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#return sequence with last and first items changed
2+
3+
#original seuqence
4+
seq1 = [4,5,6,7,8,9]
5+
6+
# assign the first element to temp
7+
temp = seq1[0]
8+
9+
#length of the sequence
10+
length = len(seq1)
11+
12+
#assign the lat element to temp1
13+
temp1 = seq1[length-1]
14+
15+
#print the original sequence
16+
print("original sequence",seq1)
17+
18+
#remove the first element
19+
seq1.pop(0)
20+
21+
#remove the last element
22+
seq1.pop(length-2)
23+
print("sequence with first and last element removed",seq1)
24+
25+
#append the first element which was stored in temp
26+
seq1.append(temp)
27+
28+
#insert the last element which was stored in temp1
29+
seq1.insert(0,temp1)
30+
print("sequence with the first and last items exchanged",seq1)
31+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#return sequence with every other items removed
2+
3+
#original seuqence
4+
seq1 = [4,5,6,7,8,9]
5+
6+
#length of the sequence
7+
length = len(seq1)
8+
9+
#print the original sequence
10+
print("original sequence",seq1)
11+
12+
n = 1
13+
lim = abs(length/2)
14+
# if sequence length is half the number of original items stop looping
15+
while (length > lim):
16+
17+
seq1.pop(n)
18+
n=n+1
19+
if (n > lim):
20+
print("sequence with every other item removed...",seq1)
21+
22+
23+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#return a sequence with the first and last 4 items removed,
2+
#and every other item in between
3+
4+
#original seuqence
5+
seq1 = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d']
6+
7+
#length of the sequence
8+
length = len(seq1)
9+
10+
#print the original sequence
11+
print("original sequence",seq1)
12+
13+
# remove the first 4 items and the last four items from the sequence
14+
seq2 = seq1[4:(length-4)]
15+
print("sequence with first and last 4 items removed",seq2)
16+
17+
n = 1
18+
length2 = len(seq2)
19+
lim = abs(length2/2)
20+
# if sequence length is half the number of original items stop looping
21+
while (length2 > lim):
22+
23+
seq2.pop(n)
24+
n=n+1
25+
if (n > lim):
26+
print("sequence with every other item in between...",seq2)
27+
28+
29+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#return a sequence reversed (just with slicing)
2+
3+
#original seuqence
4+
seq1 = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d']
5+
6+
#print the original sequence
7+
print("original sequence",seq1)
8+
9+
seq2 = seq1[::-1]
10+
print("sequence reversed is:.......",seq2)
11+
12+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#return a sequence with the middle third, then last third, then the first third in the new order
2+
3+
#original seuqence
4+
seq1 = [1,2,3,4,5,6,7,8,9,10,'a','b']
5+
6+
length = len(seq1)
7+
8+
#print the original sequence
9+
print("original sequence",seq1)
10+
11+
#a third of the length of the list is
12+
third = int(abs(length/3))
13+
14+
# split the sequence into three equal parts
15+
seq2 = seq1[0:(third)]
16+
seq3 = seq1[third:(third*2)]
17+
seq4 = seq1[(third*2):(third*3)]
18+
19+
print("first ..........3rd",seq2)
20+
print("2nd ..........3rd",seq3)
21+
print("3rd ..........3rd",seq4)
22+
23+
# combine the sequence in a different order
24+
nseq = seq3 + seq4 +seq2
25+
print("new sequence:..........",nseq)
26+
27+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# String format lab execises from session-03
2+
# produce 'file_002 : 123.46, 1e+04'
3+
4+
import random
5+
import string
6+
import sys
7+
import os
8+
import math
9+
10+
#passnum function to pass the 3 numbers
11+
def passnum(x,y,z):
12+
print("The first 3 numbers are: {:d}, {:d}, {:d}".format(x,y,z))
13+
n = ('{:d}{:d}{:d}'.format(x,y,z))
14+
return n
15+
16+
#call the function
17+
n=passnum(x=7,y=8,z=9)
18+
19+
# Convert the returned value to an integer
20+
n = int(n)
21+
f1 = '{:.4f}'.format(.4567).lstrip('0')
22+
23+
# Convert to float
24+
f = float(f1)
25+
26+
m = ('{:d}{:.4f}'.format(n,f).lstrip('0'))
27+
28+
# Convert to float
29+
m = float(m)
30+
# This was the only way for now I could get rid of the leading zero
31+
m = (m/10)+0.41103
32+
33+
strg = (2,m,10000)
34+
print("Change this............",strg)
35+
36+
a = ('{:d}'.format(strg[0]))
37+
b = ('{:05.2f}'.format(strg[1]))
38+
# Format the last value as an exponent
39+
c = ('{:01.0e}'.format(strg[2]))
40+
41+
print("To this................ file_00{} : {},".format(a,b),c,'\n')
42+

0 commit comments

Comments
 (0)