Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 25th, 2012, 12:30 PM
 broker1200
Registered User

Join Date: Oct 2012
Posts: 2
Time spent in forums: 11 m 7 sec
Reputation 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
October 25th, 2012, 01:24 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,211
Time spent in forums: 1 Month 3 Weeks 2 Days 17 h 32 m 42 sec
Reputation Power: 455
Code:
```\$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
>>> 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!

#3
October 25th, 2012, 01:59 PM
 broker1200
Registered User

Join Date: Oct 2012
Posts: 2
Time spent in forums: 11 m 7 sec
Reputation Power: 0
Quote:
 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,.........
b49P23TIvg agrees: That certainly is a puzzle.

#4
November 3rd, 2012, 01:56 AM
 eliskan
Contributing User

Join Date: Nov 2012
Posts: 43
Time spent in forums: 15 h 59 m 28 sec
Reputation 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]

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Small programme big help