Skip to content

Commit 981b67c

Browse files
author
BimaAdi
committed
add easter egg
1 parent f561193 commit 981b67c

10 files changed

Lines changed: 4281 additions & 1 deletion

File tree

app/components/sections/ticket/ticket.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EarlyBirdTicketCard } from "~/components/shared/ticket/early-bird-ticke
55
import { TicketCard } from "~/components/shared/ticket/ticket-card";
66
import { TicketErrorModal } from "~/components/shared/ticket/ticket-error-modal";
77
import { TicketPurchaseModal } from "~/components/shared/ticket/ticket-purchase-modal";
8+
import { usePython } from "~/hooks/usePython";
89
import type { CredentialsData } from "~/types/auth";
910

1011
export const Ticket = ({
@@ -21,6 +22,7 @@ export const Ticket = ({
2122
const [isErrorModalOpen, setIsErrorModalOpen] = useState(false);
2223
const [errorMessage, setErrorMessage] = useState("");
2324
const [errorTitle, setErrorTitle] = useState("Unable to purchase ticket");
25+
usePython({ pyodideURL: "/assets" });
2426

2527
const handleSelectTicket = (ticket: TicketType) => {
2628
if (!user) {

app/global.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export {};
2+
3+
declare global {
4+
interface Window {
5+
answerPy: (pythonCode: string) => void;
6+
tryPy: (pythonCode: string) => void;
7+
}
8+
}

app/hooks/usePython.tsx

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { loadPyodide } from "pyodide";
2+
import { useEffect } from "react";
3+
4+
export function usePython({ pyodideURL = undefined }: { pyodideURL?: string }) {
5+
const VOUCHER = "UFlDT05NQU5JQU1BTlRBUA==";
6+
useEffect(() => {
7+
const asyncInit = async () => {
8+
console.log("Want a free Voucher? answer the code below using python");
9+
console.log(`Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
10+
11+
You may assume that each input would have exactly one solution, and you may not use the same element twice. You must return the answer in order.`);
12+
console.log("example 1: nums: [2, 7, 11, 15] target: 9 output: [0, 1]");
13+
console.log("example 2: nums: [3, 2, 4] target: 6 output: [1, 2]");
14+
console.log("example 3: nums: [3, 3] target: 6 output: [0, 1]");
15+
console.log(
16+
"to answer just call window.answerPy function and put your python function named it `def two_sums(numbers: list[int])`. Here the example",
17+
);
18+
console.log(`window.answerPy(\`
19+
import random
20+
21+
def two_sums(nums: list[int], target: int) -> list[int]:
22+
random.shuffle(nums)
23+
return [0, 1]
24+
\`)`);
25+
console.log("You can also try run python code using window.tryPy()");
26+
console.log("window.tryPy('print(1 + 1)')");
27+
28+
const pyodide = await loadPyodide({
29+
indexURL: pyodideURL,
30+
});
31+
32+
window.tryPy = (pythonCode: string) => {
33+
pyodide.runPython(pythonCode);
34+
};
35+
36+
window.answerPy = (pythonCode: string) => {
37+
const twoSumTestCases = [
38+
{
39+
id: 1,
40+
description: "Standard Case (Basic)",
41+
input: {
42+
nums: [2, 7, 11, 15],
43+
target: 9,
44+
},
45+
expectedOutput: [0, 1],
46+
},
47+
{
48+
id: 2,
49+
description: "Unsorted Array",
50+
input: {
51+
nums: [3, 2, 4],
52+
target: 6,
53+
},
54+
expectedOutput: [1, 2],
55+
},
56+
{
57+
id: 3,
58+
description: "Duplicate Elements",
59+
input: {
60+
nums: [3, 3],
61+
target: 6,
62+
},
63+
expectedOutput: [0, 1],
64+
},
65+
{
66+
id: 4,
67+
description: "Negative Numbers Included",
68+
input: {
69+
nums: [-3, 4, 3, 90],
70+
target: 0,
71+
},
72+
expectedOutput: [0, 2],
73+
},
74+
{
75+
id: 5,
76+
description: "All Negative Numbers",
77+
input: {
78+
nums: [-10, -1, -18, -19],
79+
target: -20,
80+
},
81+
expectedOutput: [1, 3],
82+
},
83+
{
84+
id: 6,
85+
description: "Target is a Negative Number",
86+
input: {
87+
nums: [5, 20, -3, 8],
88+
target: 5,
89+
},
90+
expectedOutput: [2, 3],
91+
},
92+
{
93+
id: 7,
94+
description: "Correct Solution Uses the Last Two Elements",
95+
input: {
96+
nums: [1, 2, 3, 4, 5, 6],
97+
target: 11,
98+
},
99+
expectedOutput: [4, 5],
100+
},
101+
{
102+
id: 8,
103+
description: "Target is Zero with One Positive and One Negative",
104+
input: {
105+
nums: [10, 2, -5, -10, 7],
106+
target: 0,
107+
},
108+
expectedOutput: [0, 3],
109+
},
110+
{
111+
id: 9,
112+
description: "Large Numbers",
113+
input: {
114+
nums: [100000000, 500000, 200000000],
115+
target: 300000000,
116+
},
117+
expectedOutput: [0, 2],
118+
},
119+
{
120+
id: 10,
121+
description: "Multiple Duplicates (But Only One Valid Pair)",
122+
input: {
123+
nums: [2, 5, 5, 11],
124+
target: 10,
125+
},
126+
expectedOutput: [1, 2],
127+
},
128+
];
129+
130+
pyodide.runPython(pythonCode);
131+
const twoSumFn = pyodide.globals.get("two_sums");
132+
const answers = twoSumTestCases.map((item) => {
133+
const result = twoSumFn(item.input.nums, item.input.target).toJs();
134+
return (
135+
Array.isArray(result) &&
136+
result.length === item.expectedOutput.length &&
137+
result.every((value, index) => value === item.expectedOutput[index])
138+
);
139+
});
140+
141+
const numCorrect = answers.reduce(
142+
(prev, cur) => (cur === true ? prev + 1 : prev),
143+
0,
144+
);
145+
const boom = atob(VOUCHER);
146+
147+
if (numCorrect === 10) {
148+
console.log(
149+
`Congrats your answer is correct here the voucher: ${boom}`,
150+
);
151+
console.log(
152+
"if the voucher not working it means the quota already full",
153+
);
154+
return;
155+
}
156+
157+
console.log(
158+
`You've got ${numCorrect}/10 [${answers.map((item) => (item ? "V" : "X")).join("|")}] Please try again`,
159+
);
160+
};
161+
};
162+
163+
asyncInit();
164+
}, [pyodideURL]);
165+
}

biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"useIgnoreFile": false
77
},
88
"files": {
9-
"ignoreUnknown": false
9+
"ignoreUnknown": false,
10+
"includes": ["**", "!public/assets"]
1011
},
1112
"formatter": {
1213
"enabled": true,

package-lock.json

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dotenv": "^17.2.3",
2222
"isbot": "^5.1.32",
2323
"lucide-react": "^0.542.0",
24+
"pyodide": "^314.0.2",
2425
"qrcode.react": "^4.2.0",
2526
"react": "^19.2.3",
2627
"react-dom": "^19.2.3",

0 commit comments

Comments
 (0)