

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find position of first event number of line l of a triangle of numbers in Python
Suppose we are generating a number triangle like below
1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1
where in each row the elements are generated by adding three numbers on top of it. Now if we have a line number l. We have to find the position of first even number of that line. The position values are starting from 1.
So, if the input is like l = 5, then the output will be 2
1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1 1 4 10 16 19 16 10 4 1
To solve this, we will follow these steps −
- if l is same as 1 or l is same as 2, then
- return -1
- otherwise when l mod 2 is same as 0, then
- if l mod 4 is same as 0, then
- return 3
- otherwise,
- return 4
- if l mod 4 is same as 0, then
- otherwise,
- return 2
Example
Let us see the following implementation to get better understanding −
def solve(l): if l == 1 or l == 2 : return -1 elif l % 2 == 0: if l % 4 == 0: return 3 else: return 4 else: return 2 l = 5 print(solve(l))
Input
5
Output
2
- Related Questions & Answers
- Program to find the number of possible position in a line in Python
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- Program to find sum of first N odd numbers in Python
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of sets of k-non-overlapping line segments in Python
- Find number of magical pairs of string of length L in C++.
- Program to find the sum of first n odd numbers in Python
- Program to find number of values factors of two set of numbers
- Program to find Circumcenter of a Triangle in C++
- Python - Find the number of prime numbers within a given range of numbers
- Program to find number of ways where square of number is equal to product of two numbers in Python
- C++ program to find first digit in factorial of a number
- Java program to find the area of a triangle
- Java Program to find all angles of a triangle
Advertisements