Python Examples: Length Is 2 or More Example: Count - Words - Py
Python Examples: Length Is 2 or More Example: Count - Words - Py
Python Examples: Length Is 2 or More Example: Count - Words - Py
1. count_words.py – Given a list of strings, return the count of the number of strings where the string
length is 2 or more
Example: count_words.py
def count_strings(words):
count = 0
for word in words:
if len(word) >= 2 :
count = count + 1
# you can also use count+=1
return count
cnt = count_strings(l_words)
print "Found", cnt, "strings with length higher than 2"
Running example:
$ ./count_words.py
List: ['aba', 'xyz', 'aa', 'x', 'bbb']
Found 4 strings with length higher than 2
Exercise: Modify the script to count and print the elements of the list that have the same first and
last chars.
E.g. The exercise will print
$ ./count_words_solution.py
List: ['aba', 'xyz', 'aa', 'x', 'bbb']
aba
aa
bbb
Found 3 strings with length higher than 2
Example: add.py 3 5
#!/usr/bin/python
import sys
Exercise: Modify the script to calculate the sum of all numbers given as parameters (not just for 2
numbers)
3. mix_words.py - Given strings a and b, return a single string with a and b separated by a space '<a>
<b>', except swap the first 2 chars of each string.
Assume a and b are length 2 or more.
Example: mix_words.py
val1 = "test"
val2 = "mix"
print 'Words to mix up:',val1,’,’,val2
Running example:
$ python mix_words.py
Words to mix up: test , mix
Mixed word: mist tex
Exercise: Modify mix_words procedure to receive a dictionary as parameter. Mix up each key and
value pair of the dictionary as above( <key> <value>, swap first two chars ). Return a list that contain
all values. Print the list on the screen.
E.g The exercise will print
$ python mix_up_solution.py
Dictionary to mix up: {'mix': 'test', 'one': 'First'}
Returned list: ['tex mist', 'Fie onrst']
4. get_employee_name.py – Given a file as a parameter, extract the Name and Surname of each
employee. Write the resulted name and surname on another file.
return text
def main():
if len(sys.argv) != 2:
print 'usage: ./get_employee_name.py file-to-read'
sys.exit(1)
file_text = read_file(sys.argv[1])
print file_text
filtered_text = parse_text(file_text)
print filtered_text
write_file(filtered_text)
if __name__ == '__main__':
main()
Exercise:
1) Modify read_file procedure to read the file line by line
2) Create read_file_list procedure that reads the file line by line and returns a list
3) Create parse_list procedure that parses the list returned by read_file_list
It will return Name, Surname and Phone only for employees that have “Address” field set to
“Bucharest”
E.g. Tuple format: [(<name>,<surname>,<phone>)]
4) Modify write_file procedure to print on the first line the number of employees with “Address” as
“Bucharest” and then the Name, Surname and Phone
E.g. The output file will have the following format:
Number of employees living in Bucharest:<number>
Name: <name> <surname> Phone: <phone_number>
Example: ./employee_class.py
class Employee:
'Common base class for all employees'
# class variable that counts number of employees
empCount = 0
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
Running example:
$ python employee_class.py
Name : Emp1 , Salary: 2000
Name : Emp2 , Salary: 5000
Total Employee 2
Exercise:
1) Create a class that will inherit Employee class – Manager class.
The class will have mCount variable the will count the number of Manager objects
The class constructor will have 3 arguments: name, salary and departments
2) In Manager class create displayEmployee method that displays the name
3) In Manager class create displayManager method that prints the departments he is assigned to
4) Create 2 Manager objects. Call displayEmployee for both manager objects.
5) Print the value of empCount from Employee and Manager
client.py
#!/usr/bin/python # This is client.py file
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
Exercise: Modify the script to send a message from client to the server and print it on the screen with
appropriate label
>> [client] Hi server!
>> [server] Welcome user!