|
| 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 | +} |
0 commit comments