- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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
Advertisements