Skip to main content
Showing 1 - 10 of 206 results

Python: Given a list, your job is to find values which are surrounded by greater values on both sides. Such values are called local minima.

For example, the number '1' (present at index 1) is a local minimum in the given series: [2,1,2]

Note: Do not consider the first and last element of the list as local minima. A point is local minima if the two elements surrounding it are greater than the number. For example, there is not minim in this list: [1,2,2,2,1].

Input format: The input will be a list of numbers.
Output Format: The output should be a list of indices of the local minima stored in increasing order.

Hint: You can convert it into a numpy/pandas series to use the inbuilt functions from pandas and numpy.

Sample Input: [2,1,3,4,1,5,6,1,7]
Sample Output: [1, 4, 7]

Note: There are multiple ways to do this. Try to choose a method other than using simple loops and comparisons.

Solution:

import ast,sys
import numpy as np

input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
l=np.array(input_list)

from scipy.signal import argrelextrema
minimas=list(argrelextrema(l, np.less)[0])#store your final list here
minimas=[int(x) for x in minimas] #do not change this code, the output should be an integer list for evaluation purposes
print(minimas)

Print an n level pyramid as follows where n is a positive integer taken as input.

Each triangle will be made out of “*” symbols and the spaces will be filled by "#" symbols for each row. For example, a 3 level pyramid will look as follows:

##*##
#***#
*****

Note: There should be no space between the stars. Remember to not print any extra character or spaces or else your solution could be rejected.

Sample input: 4

Sample output:

###*###
##***##
#*****#
*******

Solution:

n=int(input())
for i in range(n):
        print('#'*(n-i-1) + '*'*(2*i+1)+'#'*(n-i-1))

Python: Print an n level reverse pyramid as follows where n is a positive integer taken as input.

Each triangle will be made out of “*” symbols and the spaces will be filled by "#" symbols for each row. For example, a 3 level reverse pyramid will look as follows:

*****
#***#
##*##

Note: There should be no space between the stars. Remember to not print any extra character or spaces or else your solution could be rejected.

Sample input: 4

Sample output:

*******
#*****#
##***##
###*###

Solution:

n=int(input())
i=n-1
while i>-1:
        print('#'*(n-i-1) + '*'*(2*i+1)+'#'*(n-i-1))
        i=i-1

The Python program to find the longest consecutive sub-string in alphabetical order in a given string.

Note that the order determination is case insensitive, i.e. the string "Ab" is considered to be in alphabetical order. You can assume that the input will not have string where the number of possible consecutive sub-strings in alphabetical order is 0. i.e. the input will not have a string like "zxec".

For example:
Sample Input 1: Hello
Sample Output 1: ello

Solution

string=input()
prevChar = ""
curr_longest = ""
longest = ""

for char in string:
    if (prevChar.lower()<=char.lower()):
        curr_longest += char
        if len(curr_longest) > len(longest):
            longest= curr_longest
    else:
        curr_longest = char
    prevChar = char
print( longest )

Python: Given a positive integer as input, you have to find whether the sum of its digits is odd or even.

The input will have a positive integer and the output should say "odd" if the sum of its digits is odd and "even" if the sum of its digits is even.

Sample Input: 1234567
Sample Output: even

Solution 1:

n=int(input())
if sum(map(int, str(n))) % 2:
    print("odd")
else:
    print("even")

Solution 2:

n=int(input())

def getSum(n):
    sum = 0
    while (n != 0):
        sum = sum + int(n % 10)
        n = int(n/10)
    return sum

sum = getSum(n)    
if(sum % 2 == 0):
    print("even")
else:
    print("odd")

Python: Print "yes" if the list is in strictly increasing order and print "no" if the list is not strictly increasing.

Problem Statement: Given a list as input, your task is to check if the list is strictly increasing. i.e. the numbers in the list should be in an increasing order. Hence, a number at a lower index should always be smaller than a number at a higher index. Assume that the list will only have positive integers. Print "yes" if the list is in strictly increasing order and print "no" if the list is not strictly increasing. 

Sample Input: [1,4,300,400,900]
Sample Output: yes

Sample Solution 1:

import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)

if (all(i < j for i, j in zip(input_list, input_list[1:]))):
   print("yes")
else:
   print("no")

You're given a tuple with 3 integers representing the three sides of a triangle.

You're given a tuple with 3 integers representing the three sides of a triangle. Based on the three sides you need to determine the type of triangle based on both angles and sides, i.e. Acute, Obtuse, or Right angled triangle along with Equilateral, Isosceles, or Scalene. See examples for more details.

Format:
Input: A single tuple containing three integer values.
Output:  A string telling the type of triangle. First print the type of triangle based on the angles and then based on the sides. So if a triangle is, say, acute and scalene, you need to print 'Acute' on the first line and 'Scalene' on the second line. If the sides are such that a triangle cannot be formed, print 'Invalid'. You need to print the following strings for each of the cases:
Acute angled triangle: 'Acute'
Right angled triangle: 'Right'
Obtuse angled triangle: 'Obtuse'

Equilateral triangle: 'Equilateral'
Isosceles Triangle: 'Isosceles'
Scalene Triangle: 'Scalene'

Examples:
Input 1:
(4, 9, 10)
Output 1:
Obtuse
Scalene
Input 2:
(3, 8, 12)
Output 2:
Invalid

Explanation: In the first example, the triangle is obtuse according to the angles and since the three sides are unequal, it is a scalene triangle.
In the second example, the 3 values are not satisfying the condition a+b < c, where c is the largest size. Hence, a triangle cannot be formed using these three sides which means the given input is 'Invalid'.

# Read the input list
import ast,sys
input_str = sys.stdin.read()
t = ast.literal_eval(input_str)

t = sorted(t)

def side_func(t):

    if(t[0] + t[1] <= t[2]):
        return('Invalid')

    elif(t[0] == t[1] and t[1] == t[2]):
        return('Equilateral')

    elif((t[0] == t[1]) or (t[1] == t[2]) or (t[0] == t[2])):
        return('Isosceles')
    else:
        return('Scalene')
        
def angle_func(t):
    if(t[2]**2 == t[0]**2 + t[1]**2):
        return('Right')
        
    elif(t[2]**2 < (t[0]**2 + t[1]**2)):
        return('Acute')
        
    else:
        return('Obtuse')
        
side = side_func(t)
angle = angle_func(t)

if(side=='Invalid'):
    print(side)
else:
    print(angle)
    print(side)

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.