odd mountain is a list of odd numbers going up from 1 and then back to 1.
e.g. odd_mountain of size 5 is [1,3,5,3,1]
odd_mountain of size 4 is [1,3,3,1]

Hint: use the list functions and a builtin function.

Can anyone help me in this? all my tries are going waste. I thought of doing it using range but getting trouble with even sizes! Please help!

Re: Odd Mountain in lists 80 80

The easiest to understand is to use two loops, the first going up to the halfway point and the second going down. You have to first decide how to find the halfway point for an even or odd number for the size of the mountain (note the use of two different values below). You can also use one loop, incrementing a counter by adding to it going up and subtracting from it going down but you then have to allow for two numbers in the middle that could be equal.

My preference would be to use list slicing. The following is not meant to be ready to hand in code but a rough example.

too_many_nums = [1, 3, 5, 7, 9, 11]

for length in (3, 4, 5, 6, 10):
    middle, remain = divmod(length, 2)
    if remain:
        middle += 1
    list_of_odds = too_many_nums[:middle]
    ## length-middle because if an odd length, the back half 
    ## has one less value than the front, i.e. they are not equal
    junk_list = too_many_nums[:length-middle]
    junk_list.reverse()
    list_of_odds.extend(junk_list)
    print length, list_of_odds
Re: Odd Mountain in lists 80 80

The easiest to understand is to use two loops, the first going up to the halfway point and the second going down. You have to first decide how to find the halfway point for an even or odd number for the size of the mountain (note the use of two different values below). You can also use one loop, incrementing a counter by adding to it going up and subtracting from it going down but you then have to allow for two numbers in the middle that could be equal.

My preference would be to use list slicing. The following is not meant to be ready to hand in code but a rough example.

too_many_nums = [1, 3, 5, 7, 9, 11]
for length in (3, 4, 5, 6, 10):
middle, remain = divmod(length, 2)
if remain:
middle += 1
list_of_odds = too_many_nums[:middle]
## length-middle because if an odd length, the back half
## has one less value than the front, i.e. they are not equal
junk_list = too_many_nums[:length-middle]
junk_list.reverse()
list_of_odds.extend(junk_list)
print length, list_of_odds

Thanks for the reply but I'm not allowed to use loops and the question says it can be done easily by using an inbuilt function :/

Re: Odd Mountain in lists 80 80

Try this:

def getOddMnt(size):
    if size % 2 == 1:
        return list(range(1, size, 2)) + list(range(size, 0, -2))
    else:
        return list(range(1, size, 2)) + list(range(size - 1, 0, -2))

No loops. Only built-in type and function.

Re: Odd Mountain in lists 80 80
def odd_mount(count):
    list1=range(1,count+1,2)
    list2=range(1,count,2)
    list2.reverse()
    list3=list1+list2
    return list3
Re: Odd Mountain in lists 80 80
def odd_mount(count):
    list1=range(1,count+1,2)
    list2=range(1,count,2)
    list2.reverse()
    list3=list1+list2
    return list3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.