0

I'm trying to find the maximum number of people at the base level after forming a human tower with the given number of people using recursion. In the human tower each level contains odd number of people and the top level contains only 1 person. The difference between any two adjacent levels is 2 persons I mean The number of people decrease from lower level to higher level by 2. Suppose if we have 4 persons then 3 will be at the bottom and 1 will be at top. if we have 6 persons then 3 will be at bottom and 1 will be at top and the remaining persons are discarded.

I tried the following code and I want to improve the code.

x=1
def human_pyramid(no_of_people):
    global x
    if(no_of_people<x):
        return x-2
    else:
        no_of_people-=x
        x+=2
        return human_pyramid(no_of_people)
print(human_pyramid(20))

If input is 20 output should be 7. If input is 10 output should be 5. If input is 1 output should be 1.

Ramesh Reddy
  • 10,159
  • 3
  • 17
  • 32

3 Answers3

0

Consider the below code

def human_pyramid(no_of_people, level=0):
    people_on_row = 2 * level + 1
    if no_of_people < people_on_row:
        return people_on_row - 2
    return human_pyramid(no_of_people - people_on_row, level + 1)

Here I avoid using a global variable, and instead pass the row number as an additional argument, of which defaults to zero.

Output:

>>> human_pyramid(20)
7
>>> human_pyramid(10)
5
>>> human_pyramid(1)
1
Dillon Davis
  • 6,679
  • 2
  • 15
  • 37
0

The required calculation can be formulated with a mathematical formula:

import math
print(int(math.sqrt(no_of_people))*2-1)
trincot
  • 317,000
  • 35
  • 244
  • 286
0
  1. Refactor your function to not use recursion. There limit on recursive calls in python and while you can change that limit, it's better to just avoid recursion IMO. From a glance, it looks like your function would fail due to this limit if passed a fairly small integer. See here for some more info.

  2. Do not use a global. Either pass that variable in, or declare it as a local variable within your function. In the contained case you've created the function may always work, but in a real-world environment the global introduces edge cases that can break the function.

  3. Don't use unclear variables names like 'x'. The variable name is your opportunity to clarify what a variable represents.

  4. Add types to the function's input variables and return type. Just so we're clear what I mean, here's an example of your updated function header:

    def human_pyramid(no_of_people: int) -> int:
    
  5. Add a docstring right below your function header.

Timothy Jannace
  • 1,401
  • 12
  • 18
  • I have to disagree on #4. For compiled languages the type system helps the compiler with optimizations and can catch errors, but for a scripting language like Python it seems to take away from its concise and flexible nature. Well written python often reads like English, which this does not. As long as you use descriptive variable names, typing shouldn't be necessary in a scripting language like Python. – Dillon Davis Jan 14 '19 at 20:11
  • 1
    @dillondavis In some senses it makes sense to compare a communication language with a programming languages, but "type" is an inherent part of programming that is not present in the former. Declaring type reduces edge cases, making code more reliable. It also informs the IDE, making it easier for a human to interact with the code. – Timothy Jannace Jan 14 '19 at 21:00
  • I still disagree with "making it easier for a human to interact with the code". The other benefits are absolutely true, but if you care about robustness more than readability + flexibility, then I would say you should be using a language that is built around its type system instead. Python is a scripting language; it specializes in the "quick and dirty" side of coding. Adding explicit types greatly decreases that flexibility. – Dillon Davis Jan 14 '19 at 23:49
  • This seems like the kind of topic that has raged on elsewhere, so no reason to keep it going here. I can respect your viewpoint and see where you're coming from a bit in regards to what you see the intended scope of python as. – Timothy Jannace Jan 15 '19 at 01:23