Giter Club home page Giter Club logo

module_5_homework's Introduction

module_5_homework: Python Practice - Session 5

Task 5.1

Open file data/unsorted_names.txt in data folder. Sort the names and write them to a new file called sorted_names.txt. Each name should start with a new line as in the following example:

Adele
Adrienne
...
Willodean
Xavier

Task 5.2

Implement a function which search for most common words in the file. Use data/lorem_ipsum.txt file as a example.

def most_common_words(filepath, number_of_words=3):
    pass

print(most_common_words('lorem_ipsum.txt'))
>>> ['donec', 'etiam', 'aliquam']

NOTE: Remember about dots, commas, capital letters etc.

Task 5.3

File data/students.csv stores information about students in CSV format. This file contains the student’s names, age and average mark.

  1. Implement a function which receives file path and returns names of top performer students
def get_top_performers(file_path, number_of_top_students=5):
    pass

print(get_top_performers("students.csv"))
>>> ['Teresa Jones', 'Richard Snider', 'Jessica Dubose', 'Heather Garcia', 'Joseph Head']
  1. Implement a function which receives the file path with students info and writes CSV student information to the new file in descending order of age. Result:
student name,age,average mark
Verdell Crawford,30,8.86
Brenda Silva,30,7.53
...
Lindsey Cummings,18,6.88
Raymond Soileau,18,7.27

Task 5.4

Look through file modules/legb.py.

  1. Find a way to call inner_function without moving it from inside of enclosed_function.

2.1) Modify ONE LINE in inner_function to make it print variable 'a' from global scope.

2.2) Modify ONE LINE in inner_function to make it print variable 'a' form enclosing function.

Task 5.5

Implement a decorator remember_result which remembers last result of function it decorates and prints it before next call.

@remember_result
def sum_list(*args):
	result = ""
	for item in args:
		result += item
	print(f"Current result = '{result}'")
	return result

sum_list("a", "b")
>>> "Last result = 'None'"
>>> "Current result = 'ab'"
sum_list("abc", "cde")
>>> "Last result = 'ab'" 
>>> "Current result = 'abccde'"
sum_list(3, 4, 5)
>>> "Last result = 'abccde'" 
>>> "Current result = '12'"

Task 5.6

Implement a decorator call_once which runs a function or method once and caches the result. All consecutive calls to this function should return cached result no matter the arguments.

@call_once
def sum_of_numbers(a, b):
    return a + b

print(sum_of_numbers(13, 42))
>>> 55
print(sum_of_numbers(999, 100))
>>> 55
print(sum_of_numbers(134, 412))
>>> 55
print(sum_of_numbers(856, 232))
>>> 55

Task 5.7*

Run the module modules/mod_a.py. Check its result. Explain why does this happen. Try to change x to a list [1,2,3]. Explain the result. Try to change import to from x import * where x - module names. Explain the result.

Materials

module_5_homework's People

Contributors

valyapradun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.