Python Forum
[Learning] Calendar without modules or list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Learning] Calendar without modules or list
#1
Sad 
EXERCISE:
The following function allows you to find out the day of the week for a specific date.
The date is supplied in the form of three integer parameters and the function returns
0 for Sunday, 1 for Monday, 2 for Tuesday, etc. Write a program
to print on the screen the calendar of a full month, corresponding to a
any month and year based on the function provided. Consider that the week
It starts on Sunday.

def diadelasemana(dia,mes,año):
if mes < 3:
mes = mes + 10
año = año – 1
else:
mes = mes – 2
siglo = año // 100
año2 = año % 100
diasem = (((26*mes-2)//10)+dia+año2+(año2//4)+(siglo//4)-(2*siglo))%7
if diasem < 0:
diasem = diasem + 7
return diasem
RESTRICTIONS:
ONLY using: if, elif, else, and, or, not, while, break, continue, for, in, range(), lambda.

NO lists or modules.

I made an incomplete program that some parts are in spanish, but I think you can get around the meaning.
# Author: KoFu

def diadelasemana(dia,mes,año):
    """ Permite averiguar el día de la semana para una fecha introducida por el usuario."""
    if mes < 3:
        mes = mes + 10
        año = año - 1
    else:
        mes = mes - 2
        siglo = año // 100
        año2 = año % 100
        diasem = (((26*mes-2)//10)+dia+año2+(año2//4)+(siglo//4)-(2*siglo))%7
    if diasem < 0:
        diasem = diasem + 7
    return diasem


def validarfecha(x,y,z):
    """ Permite validar la fecha devolviendo true or false."""
    if(y==1 or y==3 or y==5 or y==7 or y==8 or y==10 or y==12):
        max1=31
    elif(y==4 or y==6 or y==9 or y==11):
        max1=30
    elif(z%4==0 and z%100!=0 or z%400==0):
        max1=29
    else:
        max1=28
    if(y<1 or y>12):
        valida = False
    elif(x<1 or x>max1):
        valida = False
    else:
        valida = True
    return valida;


        
def graficar_calendario(x,y,z):
    
    """ Devuelve un calendario del mes de la fecha ingresada por el usuario"""
    
    # Asociamos la variable max1 con la cant de dias del los meses correspondientes
    if(y==1 or y==3 or y==5 or y==7 or y==8 or y==10 or y==12):
        max1=31
    elif(y==4 or y==6 or y==9 or y==11):
        max1=30
    elif(z%4==0 and z%100!=0 or z%400==0):
        max1=29
    else:
        max1=28
        
    ## Empieza a graficar
    
    if (y==1):
        print ("Enero")
    elif (y==2):
        print ("Febrero")
    elif (y==3):
        print ("Marzo")
    elif (y==4):
        print ("Abril")
    elif (y==5):
        print ("Mayo")
    elif (y==6):
        print ("Junio")
    elif (y==7):
        print ("Julio")    
    elif (y==8):
        print ("Agosto")
    elif (y==9):
        print ("Septiembre")
    elif (y==10):
        print ("Octubre")
    elif (y==11):
        print ("Noviembre")
    elif (y==12):
        print ("Diciembre")
    

    print ("Do","Lu","Ma","Mi","Ju","Vi","Sa")
#    for i in range(1, max1+1, 1):
#        if (diadelasemana(1,y,z) == 0):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 1):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 2):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 3):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 4):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 5):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 6):
#            print (0+i, end= " ")
#            x +=1
#        if (diadelasemana(1,y,z) == 7):
#            print (0+i, end= " ")
#            x +=1


    for i in range(1, max1+1, 7):
        if (diadelasemana(1,y,z) == 0):
            print (0+i)
    
    for i in range(2, max1+1, 7):    
        if (diadelasemana(1,y,z)+1 == 1):
            print (0+i)
            
    
    for i in range(3, max1+1, 7):   
        if (diadelasemana(1,y,z)+2 == 2):
            print (0+i)
            
    
    for i in range(4, max1+1, 7):  
        if (diadelasemana(1,y,z)+3 == 3):
            print (0+i)
    
    
    for i in range(5, max1+1, 7):   
        if (diadelasemana(1,y,z)+4 == 4):
            print (0+i)
            
    
    for i in range(6, max1+1, 7):   
        if (diadelasemana(1,y,z)+5 == 5):
            print (0+i)
            
    
    for i in range(1, max1+1, 7):   
        if (diadelasemana(1,y,z)+6 == 6):
            print (0+i)
            
    
    for i in range(1, max1+1, 7):   
        if (diadelasemana(1,y,z)+7 == 7):
            print (0+i)
            
        
            
# Programa principal
undia=int(input("Ingrese el DIA: "))
unmes=int(input("Ingrese el MES: "))
unaño=int(input("Ingrese el AÑO: "))
validacion = validarfecha(undia,unmes,unaño)
if validacion == True:
    graficar_calendario(undia,unmes,unaño)
else:
    print ("La Fecha ingresada es INVALIDA")
    

# FIN
I'm stuck and I can't progress... Any help with the restrictions taken into account is much appreciated!
Reply
#2
Best is to start with plan. Check your gear and think how to use it to achieve desired result.

One possible way: there is function provided which gives weekday of given date. One can use it in way that 'dia' is set in function body to 1. This way the weekday of the first day in month is known.

Now one have to find how many days in month and construct an output. If you want to blow your teachers mind Big Grin you can use this line to determine days in a 'mes' of given 'ano':

dias_en_mes = [[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i != 2 else [28, 29][ano % 4 == 0 and ano % 100 != 0 or ano % 400 == 0] for i in range(1 ,13)][mes - 1]


However, not sure that list comprehension is allowed.

Now one knows the weekday of the first day in month and total number of days then simply:

>>> for i in range(dias_en_mes):
...     print(f'Date: {i + 1}, weekday: {(i - diasem) % 7}')


In my understanding this not compliant solution to assignment (using list comprehension and printing out numbers of weekdays, not weekdays themselves) but maybe you can get some ideas how to approach the problem at hand.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
def schwerdtfegers_method(year, month, day):
    """
    Implementation of Schwerdtfeger's_method
    of Julian calendar.
    
    Look here:
    https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Schwerdtfeger's_method
    """
    m_table = [None, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    if month >= 3:
        c = year // 100
        g = year - 100 * c
    else:
        c = (year - 1) // 100
        g = year - 1 - 100 * c
    f = c % 7
    w = (day + m_table[month] + f + g + ( g // 4)) % 7
    return days[w]
If you want to return the weekday as number, where Sunday is 0, you have to add 1 to the result w.
Otherwise 0 is Monday. Instead of returning a number, I returned a string with the weekday.

You should test this algorithm if it's implemented correct.
I've tested it with only 4 dates.

If this works, you can implement the other algorithms.
I think all of them are working with tables.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Sep-09-2019, 09:51 AM)perfringo Wrote: Best is to start with plan. Check your gear and think how to use it to achieve desired result.

One possible way: there is function provided which gives weekday of given date. One can use it in way that 'dia' is set in function body to 1. This way the weekday of the first day in month is known.

Now one have to find how many days in month and construct an output. If you want to blow your teachers mind Big Grin you can use this line to determine days in a 'mes' of given 'ano':

dias_en_mes = [[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i != 2 else [28, 29][ano % 4 == 0 and ano % 100 != 0 or ano % 400 == 0] for i in range(1 ,13)][mes - 1]


However, not sure that list comprehension is allowed.

Now one knows the weekday of the first day in month and total number of days then simply:

>>> for i in range(dias_en_mes):
...     print(f'Date: {i + 1}, weekday: {(i - diasem) % 7}')


In my understanding this not compliant solution to assignment (using list comprehension and printing out numbers of weekdays, not weekdays themselves) but maybe you can get some ideas how to approach the problem at hand.

Thanks for the help!
But no, he doesn't allow us to use a list in this exercise because at this point we didn't learn them yet. We are supposed to resolve this using the previous connectors and conditionals.
I think is to make us think about how to resolve it without any help from the system.
Thanks anyway for the logic analysis of the problem. It cleared my head a little bit.

(Sep-09-2019, 11:37 AM)DeaD_EyE Wrote:
def schwerdtfegers_method(year, month, day):
    """
    Implementation of Schwerdtfeger's_method
    of Julian calendar.
    
    Look here:
    https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Schwerdtfeger's_method
    """
    m_table = [None, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    if month >= 3:
        c = year // 100
        g = year - 100 * c
    else:
        c = (year - 1) // 100
        g = year - 1 - 100 * c
    f = c % 7
    w = (day + m_table[month] + f + g + ( g // 4)) % 7
    return days[w]
If you want to return the weekday as number, where Sunday is 0, you have to add 1 to the result w.
Otherwise 0 is Monday. Instead of returning a number, I returned a string with the weekday.

You should test this algorithm if it's implemented correct.
I've tested it with only 4 dates.

If this works, you can implement the other algorithms.
I think all of them are working with tables.

I think it might work, but sadly I can't use any lists resolve this exercise :(
Thanks anyway!
Reply
#5
You've got the number of days in the month. Get the day of the week of the first day of the month, store that in day_of_week. Print out the first:

print('   ' * day_of_week, ' 1', end = '')
The end parameter will keep the output from going to the next line.

For each day after that, increase day of the week by one, and print that date with one space before it (two if it is less than 10).

Then, if day_of_week is 6 (Saturday), print without end (to go to the next line), and reset day_of_week to 0.

Continue until the last day of the month.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Sep-09-2019, 01:40 PM)KoFu Wrote: But no, he doesn't allow us to use a list in this exercise because at this point we didn't learn them yet.

Then remove the list :-)
def schwerdtfegers_method(year, month, day):
    """
    Implementation of Schwerdtfeger's_method
    of Julian calendar.
     
    Look here:
    https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Schwerdtfeger's_method
    """
    if month == 1:
        m = 0
    elif month == 2:
        m = 3
    elif month == 3:
        m = 2
    ... # fill in the rest
    # then remove m_table
    # m_table = [None, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
    if month >= 3:
        c = year // 100
        g = year - 100 * c
    else:
        c = (year - 1) // 100
        g = year - 1 - 100 * c
    f = c % 7
    return ((day + m + f + g + ( g // 4)) % 7) + 1
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Task calendar problem humanical 8 1,588 Sep-04-2023, 02:55 PM
Last Post: Pedroski55
  Calendar program louienyy 2 4,872 Mar-30-2020, 01:21 PM
Last Post: louienyy
  Calendar calculations frequency 10 5,533 Nov-13-2018, 07:34 PM
Last Post: frequency
  Calendar calcualtions deusvult 11 8,209 May-01-2017, 02:56 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020