Sign in

Forward and backward iterations in Python

Imagine your in a situation that needs to go through the elements of a list in backward order. To achieve this we need to read the last element first and then the last but one and so on till the element at index 0. Various python programming features can be used to achieve this.

Example

list = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
for i in range( 0, len(list)) :
print(list[i])

Output

Mon
Tue
Wed
Thu
Fri

Revers the list:

Here list comprehension is used that is [::-1]. Here we are slicing the list which…


Problem statement: Given a string s consisting of open and closed brackets "(" and ")", return the length of the longest substring in s that is a valid string of parentheses.

Example 1

Input

s = ")(())(()"

Output

4

Explanation

"(())" is the longest substring with valid parentheses.

Naive approach: Find all the substrings of a given string. For every string, check if it is a valid string or not. If valid and length is more than maximum length so far, then update maximum length. We can check whether a substring is valid or not in linear time using a stack. …


Approach1:

Simple algorithm

  • n := row count of matrix
  • if n <= 2, then
  • return sum of all elements in matrix
  • first_row := sum of first row
  • last_row := sum of last row
  • diagonal = sum of matrix[i, n-1-i] for all i from 1 to n-2
  • return first_row + last_row + diagonal
class SumZshape:
def solve(self, matrix):
n = len(matrix)
if n <= 2:
return sum(sum(row) for row in matrix)
first_row = sum(matrix[0])
last_row = sum(matrix[n-1])
diagonal = sum(matrix[i][n-1-i] for i in range(1, n-1))
return first_row + last_row + diagonal

Approach 2:

Let’s add the horizontal parts of the Z…


Question: You are given a lowercase alphabet string text. Return a new string where every character in text is mapped to its reverse in the alphabet, so that a becomes z, b becomes y, c becomes x, and so on.

Example 1

Input

text = "abcdef"

Output

"zyxwvu"

Algorithm

Find a new string where every character in text is mapped to its reverse in the alphabet. As an example, a becomes z, b becomes y and so on.

So, if the input is like “abcdefg”, then the output will be “zyxwvut”

To solve this, we will follow these steps −

  • N :=…

Given two integers x, and y return the number of positions where their values differ in their binary representations as a 32-bit integer.

Example 1

Input

x = 9
y = 5


OOP is the most popular way of analysing, designing, and developing application systems, especially on complex scientific computing software systems. The perspective that views the elements of a given situation by decomposing them into objects and object relationships. This approach used to design modular and reusable software systems.

What is Object-Oriented Design?

The classical view of a computer program is that of a process that has been encoded in a form that can be executed on a computer. …


Computer Science actually came from Mathematics. Many mathematicians are programmers that have written their programs in the words of Mathematics, using mathematical notation.

As mathematicians’ problem-solving techniques became more sophisticated and the problems they were solving became more complex, they were interested in finding automated ways of solving these problems that lead to the invention of the computer.


How to do this using programming?

What on earth is a pangram?

A pangram is a sentence or expression that uses all the letters of the alphabet. … The best known pangram in English is “The quick brown fox jumps over the lazy dog,” a sentence that’s often used for touch-typing practice

The quick brown fox jumps over the lazy dog

Problem: Given a string s, representing a sentence, return whether every letter (case-insensitive) of the alphabet is used at least once.

The first naive approach you will be thinking

  1. Iterate over the entire sentence and check each character equivalent to ascii character
  2. Check if a-z or A-Z if…

What is the fuzz about microcontrollers?

The generic definition of a microcontroller is a small, low-cost and self-contained computer-on-a-chip that can be used as an embedded system. A microcontrollers work at clock rate frequencies, which usually include:

  • An 8 or 16 or 32 or 64 bit microprocessor.
  • A little measure of RAM.
  • Programmable ROM and flash memory.
  • Parallel and serial I/O.
  • Timers and signal generators.
  • ADC and DAC

Microcontrollers usually must have low-power requirements since many devices they control are battery-operated. Microcontrollers are used in many consumer electronics, car engines, computer peripherals and test or measurement equipment. And these are…


Linear programming is a special case of mathematical programming, also known as mathematical optimization. Linear optimization is a mathematical programming technique to obtain the best result or outcome, like maximum profit or least cost, in a mathematical model whose requirements are represented by linear relationships.

Pillars of LP:

  • Objective Function: Maximize or minimize, is the objective function of linear programming. For example: In the problem shown below, Z (to minimize) is the objective function.
  • Decision Variables: decides the output as decision variables. They are the unknowns of the mathematical programming model. For example: In the below problem, we are to…

Iqram Ali

Lead, Product Development, Embedded System Specialist/Mad scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store