-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.fivesixteensvgt.py
97 lines (85 loc) · 5.12 KB
/
ai.fivesixteensvgt.py
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
# I have written the same program in Python, Ruby, and Go for the Apache Maven architecture. The program creates a web page that has a background scripted element that generates and stores as a vector a random list of words, store the words in an array, and output the selections as 16 separate buttons on the webpage, each button containing at least 3 of those many words to the user. The program will make the users choose up to 5 of a total of 16 (up to 5 out of 16 choices) and put their choices in a second table array, and using vectors display them as text, generated as svg drawn as close to how the letters appear on an interactive web page. Here are the codes:
# This file is for Python
import random
import string
# A function to generate a random word of length n
def random_word(n):
word = "" # Initialize the word
alphabet = string.ascii_lowercase # Define the alphabet
for i in range(n):
r = random.randint(0, 25) # Generate a random index
word += alphabet[r] # Append a random letter
return word # Return the word
# A function to generate a random list of words of size m
def random_list(m):
list = [] # Initialize the list
for i in range(m):
n = random.randint(1, 10) # Generate a random word length between 1 and 10
list.append(random_word(n)) # Generate a random word and add it to the list
return list # Return the list
# A function to create a button with a label
def create_button(label):
print(f"<button onclick=\"select(this)\">{label}</button>") # Print the HTML code for the button
# A function to create a text element with a content
def create_text(content):
print(f"<text x=\"50%\" y=\"50%\" text-anchor=\"middle\" alignment-baseline=\"middle\" font-size=\"32\">{content}</text>") # Print the SVG code for the text
# A function to join three words with spaces
def join_words(word1, word2, word3):
return word1 + " " + word2 + " " + word3 # Return the joined words
# A global variable to store the user's choices
choices = []
choice_count = 0
# A global variable to store the timer value
timer = 5
# A function to handle the user's selection of a button
def select(button):
global choice_count # Use the global variable
if choice_count < 5: # If the user has not selected 5 buttons yet
choices.append(button) # Add the button's label to the choices list
choice_count += 1 # Increment the choice count
button.disabled = True # Disable the button
if choice_count == 5: # If the user has selected 5 buttons
start_timer() # Start the timer and display it
# A function to start the timer and display it
def start_timer():
global timer # Use the global variable
print("<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\">") # Print the SVG tag
create_text(str(timer)) # Create a text element with the timer value
print("<animate id=\"timer\" attributeName=\"content\" values=\"5;4;3;2;1\" keyTimes=\"0;0.2;0.4;0.6;0.8;1\" dur=\"5s\" fill=\"freeze\"/>") # Animate the text to change the timer value
print("<set attributeName=\"display\" to=\"none\" begin=\"timer.end+1s\"/>") # Hide the text after the timer ends and waits for 1 second
print("</svg>") # Print the closing SVG tag
print("<script>") # Print the script tag
print("setTimeout(function() { display_choices(); }, 6000);") # Set a timeout to display the choices after 6 seconds
print("</script>") # Print the closing script tag
# A function to display the user's choices as text
def display_choices():
print("<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\">") # Print the SVG tag
for i in range(5): # For each choice
create_text(choices[i]) # Create a text element with the choice
print(f"<animateTransform attributeName=\"transform\" type=\"translate\" from=\"0 0\" to=\"0 {(i - 2) * 20}\" dur=\"1s\" fill=\"freeze\"/>") # Animate the text to move vertically
print("</svg>") # Print the closing SVG tag
# The main function
def main():
random.seed() # Initialize the random seed
list = random_list(16) # Generate a random list of 16 words
print("<!DOCTYPE html>") # Print the HTML declaration
print("<html>") # Print the HTML tag
print("<head>") # Print the head tag
print("<script>") # Print the script tag
print(select) # Print the Python code for the select function as JavaScript
print(display_choices) # Print the Python code for the display_choices function as JavaScript
print("</script>") # Print the closing script tag
print("</head>") # Print the closing head tag
print("<body>") # Print the body tag
random.shuffle(list) # Shuffle the list of words
for i in range(4): # For each row of buttons
for j in range(4): # For each column of buttons
k = i * 4 + j # Get the index of the word
label = join_words(list[k], list[(k+1)%16], list[(k+2)%16]) # Join three words as the label
create_button(label) # Create a button with the label
print("<br>") # Print a line break
print("</body>") # Print the closing body tag
print("</html>") # Print the closing HTML tag
# Call the main function
if __name__ == "__main__":
main()