Skip to content

Commit cb2a52f

Browse files
committed
[Add] compare complexity code
1 parent 9536576 commit cb2a52f

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

src/components/CodeEditor.tsx

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {usePython} from "react-py";
66

77
import PythonIcon from '../../static/img/python-icon.svg'
88
import { Tab } from '@headlessui/react'
9-
import { ArrowPathIcon, PlayIcon, StopIcon } from '@heroicons/react/24/solid'
9+
import { ArrowPathIcon, PlayIcon, StopIcon, CogIcon } from '@heroicons/react/24/solid'
1010

1111
import Input from './Input'
1212
import Controls from './Controls'
@@ -68,18 +68,47 @@ const editorOnLoad = editor => {
6868

6969
export default function CodeEditor(props) {
7070
const [main, setMain] = useState(props.codes);
71+
const [compareCode, setCompareCode] = useState('');
72+
const [envCode, setEnvCode] = useState('');
73+
const [headerCode, setHeaderCode] = useState('');
74+
const [footerCode, setFooterCode] = useState('');
75+
const [isDataLoaded, setIsDataLoaded] = useState(false);
7176

7277
useEffect(() => {
7378
setShowOutput(false);
7479
setMain(props.codes);
7580
}, [props.codes]);
7681

82+
useEffect(() => {
83+
const fetchCodes = async () => {
84+
try {
85+
const compareCode = await fetch('/codes/python-compare').then(response => response.text());
86+
const envCode = await fetch('/codes/python-env').then(response => response.text());
87+
const headerCode = await fetch('/codes/python-header').then(response => response.text());
88+
const footerCode = await fetch('/codes/python-footer').then(response => response.text());
89+
90+
if (compareCode.startsWith('<!DOCTYPE html>') || envCode.startsWith('<!DOCTYPE html>')) {
91+
throw new Error('Invalid file content: Received HTML instead of Python code');
92+
}
93+
94+
setCompareCode(compareCode);
95+
setEnvCode(envCode);
96+
setHeaderCode(headerCode);
97+
setFooterCode(footerCode);
98+
setIsDataLoaded(true); // 데이터 로드 완료 설정
99+
} catch (error) {
100+
console.error('Error fetching Python codes:', error);
101+
}
102+
};
103+
fetchCodes();
104+
}, []);
105+
77106
const [selectedTab, setSelectedTab] = useState(0);
78107

79108
const handleTabChange = (index) => {
80109
setSelectedTab(index);
81110
};
82-
111+
83112
const [showOutput, setShowOutput] = useState(false)
84113

85114
const {colorMode} = useColorMode();
@@ -97,7 +126,18 @@ export default function CodeEditor(props) {
97126
} = usePython();
98127

99128
function run() {
100-
runPython(main[selectedTab])
129+
const pythonCode = `
130+
${headerCode}
131+
${props.compare}
132+
${main[selectedTab]}
133+
func = getattr(Solution(), func_name)
134+
135+
if isinstance(datas[0], tuple):
136+
func(*datas[0])
137+
else:
138+
func(datas[0])
139+
`
140+
runPython(pythonCode)
101141
setShowOutput(true)
102142
}
103143

@@ -106,9 +146,24 @@ export default function CodeEditor(props) {
106146
setShowOutput(false)
107147
}
108148

109-
function reset() {
149+
function compare() {
110150
setShowOutput(false)
111-
setMain(main)
151+
const pythonCode = `
152+
${headerCode}
153+
import sys
154+
import types
155+
156+
virtual_files = {
157+
${main.filter(code => code != 'undefined').map((code, i) => `"${props.names[i]}": '''${headerCode}\n${code}'''`).join(",\n")}
158+
}
159+
${envCode}
160+
${main.filter((code, i) => (code != 'undefined') || (i != selectedTab)).map((code, i) => `from ${props.names[i]} import Solution as Solution_${props.names[i]}\nsolutions.append(Solution_${props.names[i]}())`).join("\n\n")}
161+
${compareCode}
162+
${props.compare}
163+
${footerCode}
164+
`
165+
runPython(pythonCode)
166+
setShowOutput(true)
112167
}
113168

114169
const updateArrayItem = (index: number, newValue: string) => {
@@ -150,10 +205,10 @@ export default function CodeEditor(props) {
150205
},
151206
{ label: 'Stop', icon: StopIcon, onClick: stop, hidden: !isRunning },
152207
{
153-
label: 'Reset',
154-
icon: ArrowPathIcon,
155-
onClick: reset,
156-
disabled: isRunning
208+
label: 'Compare',
209+
icon: CogIcon,
210+
onClick: compare,
211+
disabled: isRunning || !isDataLoaded
157212
}
158213
]}
159214
isAwaitingInput={isAwaitingInput}

src/theme/CodeBlock/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function CodeBlock({ children: rawChildren, ...props }) {
2323
const [names, setNames] = useState([]);
2424
const [members, setMembers] = useState([]);
2525

26+
const children = maybeStringifyChildren(rawChildren);
2627
const axios = require('axios');
2728

2829
const fetchMembers = async (org = "Code-Study") => {
@@ -101,15 +102,14 @@ export default function CodeBlock({ children: rawChildren, ...props }) {
101102
console.error('Error fetching codes:', error);
102103
}
103104
};
104-
105105
useEffect(() => {
106106
fetchCodes();
107107
}, [props.metastring]);
108108

109109
return (
110110
<>
111111
{codes.length > 0 ? (
112-
<CodeEditor names={names} codes={codes} showButtons />
112+
<CodeEditor names={names} codes={codes} compare={children} showButtons />
113113
) : (
114114
<CodeEditor names={["Code-Study"]} codes={["No code has been solved yet!"]} showButtons />
115115
)}

0 commit comments

Comments
 (0)