4 Python Algorithms for Coding Interviews

by admin
4 Python Algorithms for Coding Interviews

[ad_1]

And Their Answer

Photo by Headway on Unsplash

Whether you want to be a data scientist, software developer, or any other position that involves coding, you may face a coding challenge.

Learning to code is like learning a new language. You need to develop a neural pathway in your mind in this language.

For example, there are stages when learning a new language.

  • Dictionary
  • Listening
  • Writing
  • Speaking

You need to master four of these stages to understand clearly and speak fluently.

In Python, schedules are of course different.

  • Data types and operators
  • Data structures
  • Control flows
  • Conditional statements
  • List comprehension
  • Loops
  • Functions
  • Object oriented programming
  • Data structures and algorithms

And many advanced topics.

If you want speak Pythonyou must be proficient in these topics first.

One of the main things to do for me is solving coding challenges. Because it will help you increase your coding challenge in your interview as well.

Where?

Here are 4 websites to sharpen your coding skills.

  • Hacker rank
  • Dataquest
  • Machine hack
  • He will eat

If you want to see more details, you can read this article.

Content Table· Introduction
·
1. Numpy Array Shaper
∘ Sample Input
∘ Sample Output
∘ Answer
∘ Decoding Steps
· 2. Leap Year Finder
∘ Answer
∘ Decoding Steps
∘ Takeaway
· 3. Prime Finder
∘ Answer
∘ Decoding
· 4. List Comprehension
∘ Answer
∘ Decoding
· Conclusion

Write code that will take 9 numbers as input and turn it into a 3×3 array.

Now, after that explanation, almost every time they either describe or give an explanation of an example input and an example output.

Sample entry

1 2 3 4 5 6 7 8 9

Sample output

Image by the author

Answer

Image by the author

Now let’s load the library and get the input.

Decoding steps

First we need to divide the given input and secondly convert them to numbers.

Third, we’ll change the type to array.

Fourth, we’re going to change the shape of this array.

For more Numpy features;

Now, in this challenge, we will determine the given year whether it is a leap year or not.

If the year is a leap year, the code should return True or False.

Leap Year Terms;

  • If the year is evenly divisible by 4, it is a leap year.
  • However, an additional rule of thumb, if a year is evenly divisible by 4 and 100, it is not a leap year unless the year is also evenly divisible by 400.

Let me give an example;

  • 1500, 1700 are both evenly divisible by 4 and 100, but not by 400, so they are not leap years.
  • On the other hand, it is 1600.

Now it is obvious that we are going to write a function containing an if else block.

Before looking for the answer, try to write your own, it’s actually like a game and quite fun.

Answer

Image by the author

Decoding steps

This challenge requires 4 if-elif blocks.

First, if the number is evenly divisible by ;

  • 4 but not 100.
  • Second 4 and 100 and 400.
  • Third 4 and 100 but not 400.
  • Fourth is also not 4.

Take-out

  • When solving this kind of challenge, it is important to separate the problem parts and tackle one problem at a time.
Photo by John Carlo Tubeleza on Unsplash

Prime numbers are whole numbers that have only two divisors: 1 and the number itself.

The first four prime numbers are 2, 3, 5, and 7.

For example, 8 has four divisors: 1, 2, 4, 8.

We know that 8 is not just a number.

Now we will write code to check whether the numbers in the list are prime numbers or not.

If the numbers are primarythe code should be printed

* “( ) is a prime number.”

If the number is not just a number should be printed

  • “( ) is not a prime number because ( ) is a divisor of ( )

Answer

Image by the author

Decoding

Now to solve this answer we need two loops.

If our first cycle can divide our second cycle, the number is automatically not prime because it has a separator, therefore we will print;

is not a prime number because { } is a divisor of { }.

We need to fill the black correctly with the separator and our number.

If our first block doesn’t respond, then our number has no delimiter, our second loop checks to see if a possible delimiter approaches our number.

Suppose you are a teacher and you are adding your student’s notes to your dictionary and you want to find the name of the student whose grade is above 90.

Answer

Decoding

Select the name in this dictionary item if the result is greater than and equal to 90.

Now of course the list goes on and on.

Another tip for you, if you are already learning one Python library or concept, you can filter the challenges on Hackerrank and then focus on that.

On the other hand, if you are at the bottom of the ladder, first try to learn all the prerequisites I gave you as a list in the introduction.

And one additional piece of information comes from my experience, if you want to be really good at any programming language, you need to write code every day.

In the early days of learning coding days I really struggled to do this, but try to find a fun way to code.

Personal projects and coding websites really help keep me consistent.

You have to be consistent to build a neural pathway in your mind and do repetitions.

Repetition is the mother of learning, the father of action, making it the architect of achievement.” Zig Ziglar

Thanks for reading my article.

“Machine learning is the last invention humanity will ever have to make.” Nick Bostrom

[ad_2]

Source link

You may also like