-1

a program that asks user to enter number of people their birth year date and month and sorts it
i got the algorithm figured out its just the code thats not hitting me!

4
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by Lardmeister
0

just write down some code and then try to enhance it step by step. then come back here and ask your questions.

what is yor algorithm or logic?

0

ask the user-for year month and date
if the year is greater than the succeding one it will swap it
once this step is done we set the new array according to year
then we check if the year is same or not
if its same we apply the same method to sort the month annd sort it out
then again we set the new arry according to month and year
after this we check if the year and month is same or not
if it is we check the date and sort it
the final array is then printed out

0

Start with some code for the

ask the user-for year month and date

part, you need the input() function (in python 3) or raw_input() (in python 2).

1

Sorting lists is easy in Python:

'''sort_list_lists1.py
sort a list of lists by different value index in the lists
'''

# test list of [name, year, month, day] lists
mylist = [
['Bob', 1994, 11, 12],
['Zoe', 1992, 3, 21],
['Amy', 1993, 5, 16]
]

print("Unsorted:")
print(mylist)

# sort by name
mylist_name = sorted(mylist)

# default sort is by index 0 here name
print("sorted by name:")
print(mylist_name)

# year is at index 1 (lists start with index 0)
mylist_year = sorted(mylist, key=lambda q: q[1])

print("sorted by year:")
print(mylist_year)

'''my result >>>
Unsorted:
[['Bob', 1994, 11, 12], ['Zoe', 1992, 3, 21], ['Amy', 1993, 5, 16]]
sorted by name:
[['Amy', 1993, 5, 16], ['Bob', 1994, 11, 12], ['Zoe', 1992, 3, 21]]
sorted by year:
[['Zoe', 1992, 3, 21], ['Amy', 1993, 5, 16], ['Bob', 1994, 11, 12]]
'''
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.