Решение на Навигация на Piet от Дейвид Каменов

Обратно към всички решения

Към профила на Дейвид Каменов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 8 успешни тест(а)
  • 0 неуспешни тест(а)

Код

'''
Python Homework1
Created by David Kamenov
hue and saturation give information about the certain dfirection we're moving to
colours -> directions
green -> right x COORDINATE
red -> left X COORDINATE
yellow -> up Y COORDINATE
blue -> down Y COORDINATE
saturation -> back or forth the direction
light -> -1 to the direction
dark -> +1 to the direction
Special colours
Black - 000000 END
White - FFFFFF - nothing happens
#
test_touple = (1, 1)
test_codes = ('00c000', 'C0FFC0', 'C00000', 'ffffff', 'C0C000')
'''
def calculate_final_vector(vector, codes):
new_vector = list(vector)
for code in codes:
if code.upper() == 'C0FFC0':
new_vector[0] -= 1
elif code.upper() == '00C000':
new_vector[0] += 1
elif code.upper() == 'FFFFC0':
new_vector[1] -= 1
elif code.upper() == 'C0C000':
new_vector[1] += 1
elif code.upper() == 'FFC0C0' :
new_vector[0] += 1
elif code.upper() == 'C00000' :
new_vector[0] -= 1
elif code.upper() == 'C0C0FF':
new_vector[1] += 1
elif code.upper() == '0000C0' :
new_vector[1] -= 1
elif code.upper() == 'FFFFFF':
continue
elif code.upper() == '000000':
break
else:
print(f'Error, unknown color has been inputted! {new_vector}')
return None
return tuple(new_vector)

Лог от изпълнението

........
----------------------------------------------------------------------
Ran 8 tests in 0.081s

OK

История (3 версии и 3 коментара)

Дейвид обнови решението на 19.10.2022 14:50 (преди около 2 години)

+# Homework1 Introduction to Python
+# Created by Deivid Kamenov 62585
+
+# Task Description:
+# hue and saturation give information about the certain direction we're moving to
+
+# colours -> directions
+# green -> right x COORDINATE
+# red -> left X COORDINATE
+# yellow -> up Y COORDINATE
+# blue -> down Y COORDINATE
+
+# saturation -> back or forth the direction
+# light -> -1 to the direction
+# dark -> +1 to the direction
+#
+
+# Special colours
+# Black - 000000 END
+# White - FFFFFF - nothing happens
+
+# Solution function; Solution 1
+def calculate_final_vector(vector, codes):
+ # print(vector)
+ for code in codes:
+ if code == 'C0FFC0' or code == 'c0ffc0':
+ vector[0] -= 1
+ # print(f'light green {vector}')
+
+ elif code == '00C000' or code == '00c000':
+ vector[0] += 1
+ # print(f'dark green {vector}')
+
+ elif code == 'FFFFC0' or code == 'ffffc0':
+ vector[1] -= 1
+ # print(f'light yellow {vector}')
+
+ elif code == 'C0C000' or code == 'c0c000':
+ vector[1] += 1
+ # print(f'dark yellow {vector}')
+
+ elif code == 'FFC0C0' or code == 'ffc0c0':
+ vector[0] += 1
+ # print(f'light red {vector}')
+
+ elif code == 'C00000' or code == 'c00000':
+ vector[0] -= 1
+ # print(f'dark red {vector}')
+
+ elif code == 'C0C0FF' or code == 'c0c0ff':
+ vector[1] += 1
+ # print(f'light blue {vector}')
+
+ elif code == '0000C0' or code == '0000c0':
+ vector[1] -= 1
+ # print(f'dark blue {vector}')
+
+ elif code == 'FFFFFF' or code == 'ffffff':
+ # print(f'white {vector}')
+ continue
+
+ elif code == '000000' or code == '000000':
+ # print(f'black {vector}')
+ break
+
+ # default
+ else:
+ print(f'Error, unknown color has been inputted! {vector}')
+ return None
+
+ return vector

Version 2 using match/case

This version of the code will only work on Python 3.10 or later

def calculate_final_vector_v2(vector, codes): # print(vector) for code in codes: match code: case 'C0FFC0' | 'c0ffc0': vector[0] -= 1 # print(f'light green {vector}')

        case '00C000' | '00c000':
            vector[0] += 1
            # print(f'dark green {vector}')

        case 'FFFFC0' | 'ffffc0':
            vector[1] -= 1
            # print(f'light yellow {vector}')

        case 'C0C000' | 'c0c000':
            vector[1] += 1
            # print(f'dark yellow {vector}')

        case 'FFC0C0' | 'ffc0c0':
            vector[0] += 1
            # print(f'light red {vector}')

        case 'C00000' | 'c00000':
            vector[0] -= 1
            # print(f'dark red {vector}')

        case 'C0C0FF' | 'c0c0ff':
            vector[1] += 1
            # print(f'light blue {vector}')

        case '0000C0' | '0000c0':
            vector[1] -= 1
            # print(f'dark blue {vector}')

        case 'FFFFFF' | 'ffffff':
            # print(f'white {vector}')
            continue

        case '000000' | '000000':
            # print(f'black {vector}')
            break

        # default
        case _:
            print(f'Error, unknown color has been inputted! {vector}')
            continue

return vector

Дейвид обнови решението на 20.10.2022 18:48 (преди около 2 години)

# Homework1 Introduction to Python
# Created by Deivid Kamenov 62585
# Task Description:
-# hue and saturation give information about the certain direction we're moving to
+# hue and saturation give information about the certain dfirection we're moving to
# colours -> directions
# green -> right x COORDINATE
# red -> left X COORDINATE
# yellow -> up Y COORDINATE
# blue -> down Y COORDINATE
# saturation -> back or forth the direction
# light -> -1 to the direction
# dark -> +1 to the direction
#
# Special colours
# Black - 000000 END
# White - FFFFFF - nothing happens
+#
+# #
-# Solution function; Solution 1
+
+# test_touple = (1, 1)
+# test_codes = ('00c000', 'C0FFC0', 'C00000', 'ffffff', 'C0C000')
+#
+
+# Solution
def calculate_final_vector(vector, codes):
# print(vector)
+ new_vector = list(vector)
+
for code in codes:
- if code == 'C0FFC0' or code == 'c0ffc0':
- vector[0] -= 1
- # print(f'light green {vector}')
+ if code.upper() == 'C0FFC0':
+ new_vector[0] -= 1
+ # print(f'light green {new_vector}')
- elif code == '00C000' or code == '00c000':
- vector[0] += 1
- # print(f'dark green {vector}')
+ elif code.upper() == '00C000':
+ new_vector[0] += 1
+ # print(f'dark green {new_vector}')
- elif code == 'FFFFC0' or code == 'ffffc0':
- vector[1] -= 1
- # print(f'light yellow {vector}')
+ elif code.upper() == 'FFFFC0':
+ new_vector[1] -= 1
+ # print(f'light yellow {new_vector}')
- elif code == 'C0C000' or code == 'c0c000':
- vector[1] += 1
- # print(f'dark yellow {vector}')
+ elif code.upper() == 'C0C000':
+ new_vector[1] += 1
+ # print(f'dark yellow {new_vector}')
- elif code == 'FFC0C0' or code == 'ffc0c0':
- vector[0] += 1
- # print(f'light red {vector}')
+ elif code.upper() == 'FFC0C0' :
+ new_vector[0] += 1
+ # print(f'light red {new_vector}')
- elif code == 'C00000' or code == 'c00000':
- vector[0] -= 1
- # print(f'dark red {vector}')
+ elif code.upper() == 'C00000' :
+ new_vector[0] -= 1
+ # print(f'dark red {new_vector}')
- elif code == 'C0C0FF' or code == 'c0c0ff':
- vector[1] += 1
- # print(f'light blue {vector}')
+ elif code.upper() == 'C0C0FF':
+ new_vector[1] += 1
+ # print(f'light blue {new_vector}')
- elif code == '0000C0' or code == '0000c0':
- vector[1] -= 1
- # print(f'dark blue {vector}')
+ elif code.upper() == '0000C0' :
+ new_vector[1] -= 1
+ # print(f'dark blue {new_vector}')
- elif code == 'FFFFFF' or code == 'ffffff':
- # print(f'white {vector}')
+ elif code.upper() == 'FFFFFF':
+ # print(f'white {new_vector}')
continue
- elif code == '000000' or code == '000000':
- # print(f'black {vector}')
+ elif code.upper() == '000000':
+ # print(f'black {new_vector}')
break
# default
else:
- print(f'Error, unknown color has been inputted! {vector}')
+ print(f'Error, unknown color has been inputted! {new_vector}')
return None
- return vector
+ return tuple(new_vector)
+
+# print(calculate_final_vector(test_vector, test_codes))

Няколко неща за коментарите:

  • "Header" документацията в Python е прието да се прави с triple quotes - '''This is some multiline documentation'''. Не сме говорили за това, но ще се възползваме от възможността по време на следващата лекция.
  • Коментари като #default би следвало да се подразбират от кода ти. Ако е за лично удобство в началото - няма грижи, но по принцип коментари от типа на speed = 50 # Set speed to 50 са излишни in general. Твоят не е толкова "безочлив", но го казвам за да го имаш предвид. :D
  • Това са домашни и е окей да има "debug" print-ове наляво надясно, гледай да третираш проекта след няколко месеца като production код и да не оставяш много такива.

Дейвид обнови решението на 24.10.2022 15:17 (преди около 2 години)

-# Homework1 Introduction to Python
-# Created by Deivid Kamenov 62585
+'''
+Python Homework1
+Created by David Kamenov
-# Task Description:
-# hue and saturation give information about the certain dfirection we're moving to
+hue and saturation give information about the certain dfirection we're moving to
-# colours -> directions
-# green -> right x COORDINATE
-# red -> left X COORDINATE
-# yellow -> up Y COORDINATE
-# blue -> down Y COORDINATE
+colours -> directions
+green -> right x COORDINATE
+red -> left X COORDINATE
+yellow -> up Y COORDINATE
+blue -> down Y COORDINATE
-# saturation -> back or forth the direction
-# light -> -1 to the direction
-# dark -> +1 to the direction
-#
+saturation -> back or forth the direction
+light -> -1 to the direction
+dark -> +1 to the direction
-# Special colours
-# Black - 000000 END
-# White - FFFFFF - nothing happens
-#
-# #
+Special colours
+Black - 000000 END
+White - FFFFFF - nothing happens
-# test_touple = (1, 1)
-# test_codes = ('00c000', 'C0FFC0', 'C00000', 'ffffff', 'C0C000')
#
-# Solution
+
+test_touple = (1, 1)
+test_codes = ('00c000', 'C0FFC0', 'C00000', 'ffffff', 'C0C000')
+'''
+
def calculate_final_vector(vector, codes):
- # print(vector)
new_vector = list(vector)
for code in codes:
if code.upper() == 'C0FFC0':
new_vector[0] -= 1
- # print(f'light green {new_vector}')
elif code.upper() == '00C000':
new_vector[0] += 1
- # print(f'dark green {new_vector}')
elif code.upper() == 'FFFFC0':
new_vector[1] -= 1
- # print(f'light yellow {new_vector}')
elif code.upper() == 'C0C000':
new_vector[1] += 1
- # print(f'dark yellow {new_vector}')
elif code.upper() == 'FFC0C0' :
new_vector[0] += 1
- # print(f'light red {new_vector}')
elif code.upper() == 'C00000' :
new_vector[0] -= 1
- # print(f'dark red {new_vector}')
elif code.upper() == 'C0C0FF':
new_vector[1] += 1
- # print(f'light blue {new_vector}')
elif code.upper() == '0000C0' :
new_vector[1] -= 1
- # print(f'dark blue {new_vector}')
elif code.upper() == 'FFFFFF':
- # print(f'white {new_vector}')
continue
elif code.upper() == '000000':
- # print(f'black {new_vector}')
break
- # default
else:
print(f'Error, unknown color has been inputted! {new_vector}')
return None
- return tuple(new_vector)
-
+ return tuple(new_vector)
-# print(calculate_final_vector(test_vector, test_codes))