Computer Science, asked by sk815817, 10 hours ago

Please solve thissssssss

Attachments:

Answers

Answered by clementcherian
0

Answer:

Introduction

While preparing for your next Python coding round, you might have noticed that algorithms requiring to manipulate one or more lists appear rather frequently. Sooner or later, you should expect to encounter one of them during your interviews as well.

Algorithms requiring to manipulate one or more lists appear rather frequently. Sooner or later, you should expect to encounter one of them during your interviews as well.

In order to help you in the process of mastering this data structure and improve your coding skills, below I present 4 methods to replace an item in Python lists as well as four simple algorithms for you to test your skills.

Most of the classic interview questions can be solved with multiple approaches, so for each problem, try to come up with your solution first, before looking at the one I provided. Similarly to other skills, algorithmic interview is one area where you can greatly improve with consistent practice.

1. Indexing

The most straightforward way to replace an item in a list is to use indexing, as it allows you to select an item or range of items in an list and then change the value at a specific position, using the assignment operator:

For example let’s suppose you were working with the following list including six integers:

lst = [10, 7, 12, 56, 3, 14]

and you were asked to add 10 to the third integer from the left. Since lists are indexed starting from zero, you could use one of the following syntaxes to change the element with index = 2 that in our case in the number 12:

#Option 1

lst[2]= lst[2] + 10

#Option 2

lst[2]+= 10 #no need to repeat "lst[2]" then more elegant

#Option 3

lst[2] = 22 #that is 12 + 10

Now try to practice this first method solving the following problem:

PROBLEM #1

Given a non-empty list including integers ( between 1 and 9) , treat it as it was representing a non-negative unique integer and increment it by one. Return the updated list.

In the result, the digits has to be stored such that the first digit of the number obtained by the sum, is at the head of the list, and each element in the list contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.

Note that our input list is made up of four non-negative integers that together represent the integer 9999. This in an edge case, because by adding 1 to this number, you will obtain an integer with 5 digits (instead of the original 4) and a leading 1.

To account for these type of situations, the solution takes advantage of two conditional statements that start evaluating the digits from last to first (using reverse() to flip the order of the indexes). If not equal to 9, ONLY the last digit is incremented by 1 through the now familiar syntax digits += 1 and the amended list is immediately returned, without evaluating the remaining indexes.

Alternatively, if the last digit is a 9, it is replaced by a 0 and the following (second-to-last) digit is evaluated. If this is not equal to 9, then 1 is added and the amended list returned. Otherwise, if each one of following digits is a 9, then the function will return an list with a leading 1 and as many 0s as the number of the elements in the original list.

10 MCQ’s To Practice Before Your Databricks Apache Spark 3.0 Developer Exam

You put the effort. You invested money and time. Now, the exam is close and you only have one attempt…Ready to nail it?

anbento4.medium.com

Similar questions