Question

In: Computer Science

SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;' + \ '07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;' + \ '11,21-12,21=SAG;12,22-01,20=CAP;01,21-02,21=AQU;02,22-03,20=PIS;' def find_astrological

SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;' + \
        '07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;' + \
        '11,21-12,21=SAG;12,22-01,20=CAP;01,21-02,21=AQU;02,22-03,20=PIS;'
def find_astrological_sign(month, date):
    '''
    (int, int) -> str
    
    Given two int values representing a month and a date, return a
    3-character string that gives us what star sign a person born in that
    month and on that date belongs to. Use the SIGNS string (already
    defined for you at the top of this file) to figure this out.

    NOTE: A lot of string slicing to do here. The
                   information for each sign is exactly 16 characters long.
                   

    >>> find_astrological_sign(8, 24)
    'VIR'

    >>> find_astrological_sign(1, 15)
    'CAP'
    '''

Looking for a way to complete this function by accessing the string SIGNS

Solutions

Expert Solution

The below program works for all valid dates mentioned in your variable SIGNS. Just run the below python code and input your date according to the prompt. I have included comments everywhere in the program to explain how the algorithm works. The example runs are included at the end.

Python Code:

import time

SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;11,21-12,21=SAG;12,22-01,20=CAP;01,21-02,21=AQU;02,22-03,20=PIS;'


daysMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] #storing all the days in each month
signs = [''] * SIGNS.count(';') #as each entry in SIGNS is delimited by a ; character
stars = [[]] * SIGNS.count(';')




#making a separate 'signs' list and 'stars' list with all the entries in SIGNS variable and all the months containing all days respectively
i, j = 0, -1
while i < len(stars):
   stars[i] = [''] * daysMonths[i]
  
   signs[i] = SIGNS[j + 1 : SIGNS.index(';', j + 1)]
   j = SIGNS.index(';', j + 1)
  
   i = i + 1
#done




#populating all the days of the month with their repective starsigns
i = 0
while i < len(signs):
   m1, d1 = int(signs[i][0 : 2]), int(signs[i][3 : 5])
   m2, d2 = int(signs[i][6 : 8]), int(signs[i][9 : 11])
   s = signs[i][12 : ]

   y = d1 - 1
   while y < daysMonths[m1 - 1]:
       stars[m1 - 1][y] = s
       y = y + 1

   y = 0
   while y < d2:
       stars[m2 - 1][y] = s
       y = y + 1

   i = i + 1
#done




#you can input any month and date and check for the starsign
def find_astrological_sign(month, date):
   return stars[month - 1][date - 1]

m, d = [int(x) for x in input(" Enter the date and the month in a single line : ").split()]
print(find_astrological_sign(m, d))

Example Run:


Related Solutions

SIGN_GROUPS = '[ARI,LEO,SAG]-[TAU,VIR,CAP]-[GEM,LIB,AQU]-[PIS,SCO,CAN]' def get_sign_group(sign): ''' (str) -> int Given a three character string representing a...
SIGN_GROUPS = '[ARI,LEO,SAG]-[TAU,VIR,CAP]-[GEM,LIB,AQU]-[PIS,SCO,CAN]' def get_sign_group(sign): ''' (str) -> int Given a three character string representing a star sign, return which group (out of 0, 1, 2, or 3) this star sign belongs to. Use the SIGN_GROUPS string (already defined for you above) to figure out the group. i.e. As given by this string '[ARI,LEO,SAG]-[TAU,VIR,CAP]-[GEM,LIB,AQU]-[PIS,SCO,CAN]' the signs ARI, LEO and SAG are in group 0, the signs TAU, VIR, CAP are in group 1, and so on. >>> get_sign_group('ARI') 0 >>>...
SIGNS = 'ARI:03,21--04,19;TAU:04,20--05,20;GEM:05,21--06,21;CAN:06,22--07,22;' + \ 'LEO:07,23--08,22;VIR:08,23--09,22;LIB:09,23--10,23;SCO:10,24--11,20;' + \ 'SAG:11,21--12,21;CAP:12,22--01,20;AQU:01,21--02,21;PIS:02,22--03,20;' def find_astrological_sign
SIGNS = 'ARI:03,21--04,19;TAU:04,20--05,20;GEM:05,21--06,21;CAN:06,22--07,22;' + \ 'LEO:07,23--08,22;VIR:08,23--09,22;LIB:09,23--10,23;SCO:10,24--11,20;' + \ 'SAG:11,21--12,21;CAP:12,22--01,20;AQU:01,21--02,21;PIS:02,22--03,20;' def find_astrological_sign(month: int, date: int) -> str: ''' Given two int values representing a month and a date, return a 3-character string that gives us what star sign a person born in that month and on that date belongs to. Use the SIGNS string (already defined for you at the top of this file) to figure this out. NOTE: A lot of string slicing to do here. It looks like the...
SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;' + \ '07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;' + \ '11,21-12,21=SAG;12,22-01,20=CAP;01,21-02,21=AQU;02,22-03,20=PIS;' def find_astrological_sign(month, date
SIGNS = '03,21-04,19=ARI;04,20-05,20=TAU;05,21-06,21=GEM;06,22-07,22=CAN;' + \ '07,23-08,22=LEO;08,23-09,22=VIR;09,23-10,23=LIB;10,24-11,20=SCO;' + \ '11,21-12,21=SAG;12,22-01,20=CAP;01,21-02,21=AQU;02,22-03,20=PIS;' def find_astrological_sign(month, date): ''' (int, int) -> str Given two int values representing a month and a date, return a 3-character string that gives us what star sign a person born in that month and on that date belongs to. Use the SIGNS string (already defined for you at the top of this file) to figure this out. NOTE: A lot of string slicing to do here. The information for each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT