Machine heart editor

Author: kenwoodjw

Python is a must-have language for machine learning algorithm engineers, and its beauty and simplicity are compelling. So have you read the Python programming interview questions? Can you handle everything from Python basics to web crawlers? Today, Machine Heart recommends a Github project for readers.

 

In this project, author kenwoodjw prepared nearly 300 Python interview questions, along with solutions and code. The author mainly asks questions from Python basics, advanced statements, web applications, databases, and testing. Readers are encouraged to focus only on the areas they need. So far, the project has completed many basic and advanced interview questions, so this article will focus on some Python interview questions for your reference.

 

The address of the project: https://github.com/kenwoodjw/python_interview_question

 

Overall, the program has nearly 300 interview questions. Although the project is just beginning to be created, many Python interview questions already offer solutions. Sample interview questions are shown below:

Here are some interview questions and solutions:

 

  • Python based

 

  • File operations

  • Module and package

  • The data type

  • Corporate Interview questions

 

  • Python senior

 

  • Design patterns

  • System programming

 

If you want to know more about machine learning interview questions, you can read GitHub’s ML algorithm interview

 

Python based

 

What is Python? According to Guido van Rossum, creator of Python, Python is a high-level programming language whose core design concepts are legibility and the ability to easily express ideas in a few lines of code. In fact, many developers choose to learn Python in the first place because of its programming beauty, which makes coding and expressing ideas very natural.

 

File operations

 

1. If there is a file file. TXT in jsonline format with a size of about 10K, our processing method is as follows:

 

def get_lines(): l = [] with open('file.txt', 'rb') as f: for eachline in f: L.append (eachline) return lif __name__ == '__main__': for e in get_lines(): process(e) #Copy the code

 

Now you’re dealing with a 10GB file.txt file with only 4G memory. What if only the get_lines function was modified and the rest of the code was left unchanged? What are the issues that need to be considered?

 

def get_lines():        l = []        with open('file.txt','rb') as f:            data = f.readlines(60000)        l.append(data)        yield lCopy the code

There are issues to consider: with only 4 gigabytes of memory, you can’t read 10 GIGABytes of files at once. In the case of batch read data, the location of each read data should be recorded. If the batch read data is too small, too much time will be spent in the read operation.

 

Module and package

2. How do I enter the date and determine the day of the year?

Import datetimedef dayofYear (): year = input(" please enter year: ") month = input(" please enter month: ") day = input(" please enter day: ") ") date1 = datetime.date(year=int(year),month=int(month),day=int(day)) date2 = datetime.date(year=int(year),month=1,day=1) return (date1-date2).days+1Copy the code

 

The data type

 

3. How to reverse the string “aStr”?

 

print("aStr"[::-1])Copy the code

 

4. What will be the output of the following code? Will it report an error?

 

list = ['a','b','c','d','e']print(list[10:])Copy the code

 

The code will print [] without raising an IndexError. An error is reported if you try to get the members of a list with an index that exceeds the number of members. For example, trying to get list[10] and subsequent members will result in IndexError. However, when we try to get a slice of the list, starting with an index that exceeds the number of members does not generate an IndexError, but simply returns an empty list. Because no errors are reported, such bugs are hard to track down.

 

5. Write a Python code that removes duplicate elements from a list.

 

l1 = ['b','c','d','c','a','a']l2 = list(set(l1))print(l2)Copy the code

 

The sort method of the list class keeps the order unchanged:

 

l1 = ['b', 'c', 'd', 'c', 'a', 'a']l2 = list(set(l1))l2.sort(key=l1.index)print(l2)Copy the code

 

You can also write:

 

l1 = ['b', 'c', 'd', 'c', 'a', 'a']l2 = sorted(set(l1), key=l1.index)print(l2)Copy the code

You can also use traversal:

 

l1 = ['b', 'c', 'd', 'c', 'a', 'a']l2 = []for i in l1:    if not i in l2:        l2.append(i)print(l2)Copy the code

  

Corporate Interview questions

 

6. Design and implement traversal directory and subdirectory, crawl. Pyc files

The first method:

import osdef getFiles(dir, suffix):    res = []    for root, dirs, files in os.walk(dir):        for filename in files:            name, suf = os.path.splitext(filename)            if suf == suffix:                res.append(os.path.join(root, filename))    print(res)getFiles("./", '.pyc')Copy the code

 

The second method:

 

import osdef pick(obj): try: if obj.[-4:] == ".pyc": print(obj) except: return Nonedef scan_path(ph): file_list = os.listdir(ph) for obj in file_list: if os.path.isfile(obj): pick(obj) elif os.path.isdir(obj): Scan_path (obj) if __name__ = = "__main__ ': path = input (' input directory) scan_path (path)Copy the code

7. How do I invert an integer, such as -123–> -321?

 

class Solution(object): def reverse(self, x): if -10 < x < 10: return x str_x = str(x) if str_x[0] ! = "-": str_x = str_x[::-1] x = int(str_x) else: str_x = str_x[1:][::-1] x = int(str_x) x = -x return x if -2147483648 < x < 2147483647 else 0if __name__ == '__main__': s = Solution() reverse_int = s.reverse(-120) print(reverse_int)Copy the code

Python senior

 

Python high-level contains many important modules, such as functions, classes, and instances, system programming, regular expressions, network programming, and so on. With these advanced properties, Python can be used for data science, web development, machine learning, and more.

 

Design patterns

 

8. Understanding of design patterns, briefly describe the design patterns you know?

 

Design patterns are summative and optimized to build reusable solutions to programming problems that we often encounter. A design pattern is not a class or library that directly affects our code. Rather, a design pattern is more advanced, a method template that is implemented in a particular situation. The factory pattern and singleton pattern are common.

 

The application scenarios of the singleton mode are found in the following conditions: In the case of resource sharing, the performance or loss caused by resource operations, such as log files and application configuration, is avoided. Facilitates communication between resources in the case of resource control.

 

What is the difference between a generator and an iterator?

 

An iterator is a more abstract concept. Any object whose class has a next method or iter method that returns itself is iterable. It’s handy to loop over container objects like strings, lists, dict, tuples, etc. The for statement actually calls iter() on the container object. Iter () returns an iterator that defines the next() method. It iterates through the container one by one. When there are no subsequent elements, next() raises a StopIteration exception.

 

Generators are simple and powerful tools for creating iterators. They are written just like regular functions, except that yield statements are used when data needs to be returned. Generators can do everything an iterator can do, and because they automatically create iter() and next() methods, generators are particularly compact, and generators are efficient, saving memory by using generator expressions instead of list parsing. In addition to the automatic methods for creating and saving program state, a StopIteration exception is automatically raised when the generator terminates.

 

10. For decorators, can you write a timer decorator that records the execution time of a function?

 

A decorator is essentially a Python function that allows other functions to add extra functionality without making any code changes, and the return value of the decorator is also a function object.

 

import time    def timeit(func):        def wrapper():            start = time.clock()            func()            end = time.clock()            print('used:',end-start)            return wrapper    @timeit    def foo():        print('in foo()'foo())Copy the code

 

System programming

 

11. Describe the process you know about.

 

An instance of a program running on the operating system is called a process. Processes require appropriate system resources: memory, time slice, PID. To create a Process, start by importing Process from Multiprocessing. Create a Process object; When you create a Process object, you can pass parameters.

 

P = Process(target=XXX, args=(tuple,), kwargs={key: value})target =XXX Value} # The argument passed to the task functionCopy the code

 

End the process by using start() to pass the argument Demo to the function specified by the child process

 

import osfrom mulitprocessing import Processimport timedef pro_func(name, age, **kwargs): for i in range(5): Print (" The child process is running, Name = % s, age = % d, pid = % d "% (name, age, OS, getpid ())) print (kwargs) time. Sleep (0.2) if __name__ = =" __main__ ": P = Process(target=pro_func, args=(' kwargs ', 18), kwargs={'m': 20}) # start the child process by starting it () time.sleep(1)Copy the code

 

12. Tell me about your understanding of multi-process, multi-threading, and coroutines.

 

Process: a running program (code) is a process, no running code called program, process is the smallest unit of system resource allocation, process has its own independent memory space, all processes do not share data, high overhead. Thread: the smallest unit of CPU scheduling execution, also called the execution path, can not exist independently, depends on the existence of the process, a process has at least one thread, called the main thread, and multiple threads sharing memory can greatly improve the efficiency of the program. Coroutine: is a user – mode lightweight thread, the scheduling of coroutine is completely controlled by the user, coroutine has its own register context and stack. When coroutine scheduling, the register context and stack are saved in other places, and when cutting back, the previously saved register context and stack are restored. Directly operating the middle stack basically has no kernel switching overhead, and you can access global variables without locking, so the context switching is very fast.

 

There are still many questions about system programming, such as:

 

 

This article is edited by machine heart. Please contact this official account for reprinting.

✄ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Join Heart of the Machine (full-time reporter/intern) : [email protected]

Contribute or seek coverage: [email protected]

Advertising & Business partnerships: [email protected]