#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Small programme big help


    I am a management undergraduate,but we have python for our IT. we have been asked the following question in assignment..it would b big help for me if anyone give me the answer

    Question

    Write a phython Programme which prints the odd numbers between 1 and 1000 in tha following order..

    1. 999,997,995,993................5,3,1
    2. 1,999,3,997,5,995,7,993,.........
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print(list(range(10,0,-2)))
    [10, 8, 6, 4, 2]
    >>> a = list(range(10,0,-2))
    >>> while a:
    ...  print(a[0])
    ...  del a[0]
    ...  if a:
    ...   print(a[-1])
    ...   del a[-1]
    ... 
    10
    2
    8
    4
    6
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print(list(range(10,0,-2)))
    [10, 8, 6, 4, 2]
    >>> a = list(range(10,0,-2))
    >>> while a:
    ...  print(a[0])
    ...  del a[0]
    ...  if a:
    ...   print(a[-1])
    ...   del a[-1]
    ... 
    10
    2
    8
    4
    6
    >>>
    thank you ver much "b49P23TIvg"
    then 1st part of the question is ok now with you answer
    what is the code for 2nd part
    "
    Write a phython Programme which prints the odd numbers between 1 and 1000 in tha following order..

    1,999,3,997,5,995,7,993,.........

    Comments on this post

    • b49P23TIvg agrees : That certainly is a puzzle.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    2
    I would approach this by making a list of all the odd numbers, I would do that by seeing if dividing it by an even number produces a float or not, if it doesn't produce a float then the number is even, right?


    Code:
    oddList = []
    oddList2=[]
    for i in range(1000):
    	no1 = float(i)/2
    	no2 = int(i/2)
    	if not no1==no2: oddList.append(i)
    for i in range(len(oddList)):
    	oddList2.append(oddList[i])
    	oddList2.append(oddList[-i-1])
    That should do the trick. Just print oddList2 for the results. It will output the following (though I abbreviated this one with ........ obviously):

    [1, 999, 3, 997, 5, 995, 7, 993, 9, 991, 11, 989, 13, 987, 15, 985, 17, 983, 19, 981, 21, 979, 23, 977, 25, 975, 27, 973, 29, 971, ........... 19, 983, 17, 985, 15, 987, 13, 989, 11, 991, 9, 993, 7, 995, 5, 997, 3, 999, 1]

IMN logo majestic logo threadwatch logo seochat tools logo