forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Questions
More file actions
111 lines (93 loc) · 4.78 KB
/
Python Questions
File metadata and controls
111 lines (93 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
# coding: utf-8
# # 1.Write a function based on the description below and check output:
#
#
# #Python, like most languages, actually uses numbers in the background to represent individual characters in a string. For example, "a" is assigned the numeric value of 97. We call this the ordinal value. www.asciitable.com shows a table of ordinal values: the ordinal value is listed in the 'dec' column, and the actual character is listed in 'chr' column.
# You'll notice, though, that many of the characters here are weird. The first 31 are cryptic characters that have special meaning to the computer. The extended codes haven't really been used since Windows came along. Beyond these 255, the higher numbers are actually used to represent emojis.
#
# Now, think about when you're asked to create a password. Typically, there are restrictions on what characters you can use. How do you check if a password is valid? You could have a list of valid characters and check each character against that list, but that would be a really long list. Instead, let's use ordinal values.
#
# Write a function called "valid_char" that determines if a single character (a string of length one) has an ordinal value corresponding to a valid character for a password. Valid characters are any character on the keyboard except spaces. Return True if it's a valid character, False if it is not.
# #
# #Hint: you can find the ordinal value of a character using the built-in Python function ord(): ord("a") -> 97
# #
# Hint 2: the range of legal characters will be one continuous range (e.g. characters 55 through 65, not separate ranges like 55 through 65 and 69 through 79).
# You can use asciitable.com to look up what range you should use.
#
# #Below are some lines of code that will test your function.
# #You can change the value of the variable(s) to test your function with different inputs.
# #
# #If your function works correctly, this will originally
# #print: True, False, True, False for below print statements
#
# print(valid_char("a"))
# print(valid_char(" "))
# print(valid_char("!"))
# print(valid_char("☺"))
#
# In[10]:
def valid_char(char):
if ord(char) in range(33,120):
print("true")
else:
print("false")
valid_char("a")
valid_char(" ")
valid_char("!")
valid_char("🙂")
# # 2.Write a function based on the description below and check the output:
#
#
# Write a function called third_character that accepts a string as an argument and returns the third character of the string. If the user inputs a string with fewer than 3 characters, return "Too short".
# Below are some lines of code that will test your function. You can change the value of the variable(s) to test your
# #function with different inputs.
# #
# #If your function works correctly, this will originally
# #print: 1, o, and "Too short", each on a different line.
# print(third_character("CS1301"))
# print(third_character("Georgia Tech"))
# print(third_character("GT"))
#
# In[16]:
def third_character(string):
if len(string)<=2:
return "Too short"
else:
return string[2]
print(third_character("CSL310"))
print(third_character("Georgia Tech"))
print(third_character("GT"))
# # 3.Write a function based on the description below and check the output:
#
# Write a function called "num_changer" that accepts a string of digits (0-9). You should make an integer from the digits of the even indices and another number from the digits in the odd indices. Return the sum of these two numbers. You can assume the given string will have a length of at least 2 digits.
# For example, if the string was "123456", you would split this into two integers, 135 and 246. Adding them would give 381. Or if the string was "13579", you would split this into 159 and 37, then add them to get 196.
# #Below are some lines of code that will test your function.
# #You can change the value of the variable(s) to test your function with different inputs.
# #If your function works correctly, this will originally
# #print: 123456 -> 381
# string_int = "123456"
# result = num_changer(string_int)
# print(string_int + " -> " + str(result))
#
# In[24]:
def num_changer(string_int):
list_of_num = []
for element in range(0, len(string_int)):
list_of_num.append(string_int[element])
map_object = map(int, list_of_num)
new_list_of_num = list(map_object)
odd_list = []
even_list = []
for num in new_list_of_num:
if num % 2 != 0:
odd_list.append(num)
else:
even_list.append(num)
o = [str(i) for i in odd_list]
odds = int("".join(o))
e = [str(i) for i in even_list]
evens = int("".join(e))
result = odds+evens
print(string_int+"------------------------------------------------------------------------->"+ str(result))
num_changer("123456")