Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237import { test, expect } from "vite-plus/test";import detectLang from "../src/index";
test("hello world", () => { const code = detectLang('print "Hello world!"'); expect(code.language).toEqual("Python");});
test("fizz buzz", () => { const code = detectLang(`def fizzbuzz(n): if n % 3 == 0 and n % 5 == 0: return 'FizzBuzz'
elif n % 3 == 0: return 'Fizz'
elif n % 5 == 0: return 'Buzz' else: return str(n) print "\n".join(fizzbuzz(n) for n in xrange(0, 100))`); expect(code.language).toEqual("Python");});
test("variable declaration", () => { const code = detectLang("i = 1"); expect(code.language).toEqual("Python");});
test("quick sort", () => { const code = detectLang(`def quickSort(arr): less = [] pivotList = [] more = [] if len(arr) <= 1: return arr else: pivot = arr[0] for i in arr: if i < pivot: less.append(i) elif i > pivot: more.append(i) else: pivotList.append(i) less = quickSort(less) more = quickSort(more) return less + pivotList + more
a = [4, 65, 2, -31, 0, 99, 83, 782, 1]a = quickSort(a)`); expect(code.language).toEqual("Python");});
test("bubble sort", () => { const code = detectLang(`def bubble_sort(seq): """Inefficiently sort the mutable sequence (list) in place. seq MUST BE A MUTABLE SEQUENCE.
As with list.sort() and random.shuffle this does NOT return """ changed = True while changed: changed = False for i in xrange(len(seq) - 1): if seq[i] > seq[i+1]: seq[i], seq[i+1] = seq[i+1], seq[i] changed = True return seq
if __name__ == "__main__": """Sample usage and simple test suite"""
from random import shuffle
testset = range(100) testcase = testset[:] # make a copy shuffle(testcase) assert testcase != testset # we've shuffled it bubble_sort(testcase) assert testcase == testset # we've unshuffled it back into a copy`); expect(code.language).toEqual("Python");});
test("heap sort", () => { const code = detectLang(`def heapsort(lst): ''' Heapsort. Note: this function sorts in-place (it mutates the list). ''' # in pseudo-code, heapify only called once, so inline it here for start in range((len(lst)-2)/2, -1, -1): siftdown(lst, start, len(lst)-1) for end in range(len(lst)-1, 0, -1): lst[end], lst[0] = lst[0], lst[end] siftdown(lst, 0, end - 1) return lst def siftdown(lst, start, end): root = start while True: child = root * 2 + 1 if child > end: break if child + 1 <= end and lst[child] < lst[child + 1]: child += 1 if lst[root] < lst[child]: lst[root], lst[child] = lst[child], lst[root] root = child else: break`); expect(code.language).toEqual("Python");});
test("http server", () => { const code = detectLang(`from http.client import HTTPConnection conn = HTTPConnection("example.com") # If you need to use set_tunnel, do so here. conn.request("GET", "/") # Alternatively, you can use connect(), followed by the putrequest, putheader and endheaders functions. result = conn.getresponse() r1 = result.read() # This retrieves the entire contents. `); expect(code.language).toEqual("Python");});
test("floyd warshall algorithm", () => { const code = detectLang(`from math import inf from itertools import product def floyd_warshall(n, edge): rn = range(n) dist = [[inf] * n for i in rn] nxt = [[0] * n for i in rn] for i in rn: dist[i][i] = 0 for u, v, w in edge: dist[u-1][v-1] = w nxt[u-1][v-1] = v-1 for k, i, j in product(rn, repeat=3): sum_ik_kj = dist[i][k] + dist[k][j] if dist[i][j] > sum_ik_kj: dist[i][j] = sum_ik_kj nxt[i][j] = nxt[i][k] print("pair dist path") for i, j in product(rn, repeat=2): if i != j: path = [i] while path[-1] != j: path.append(nxt[path[-1]][j]) print("%d → %d %4d %s" % (i + 1, j + 1, dist[i][j], ' → '.join(str(p + 1) for p in path))) if __name__ == '__main__': floyd_warshall(4, [[1, 3, -2], [2, 1, 4], [2, 3, 3], [3, 4, 2], [4, 2, -1]])`); expect(code.language).toEqual("Python");});
test("ludic numbers", () => { const code = detectLang(`def ludic(nmax=100000): yield 1 lst = list(range(2, nmax + 1)) while lst: yield lst[0] del lst[::lst[0]]
ludics = [l for l in ludic()]
print('First 25 ludic primes:') print(ludics[:25]) print("\nThere are %i ludic numbers <= 1000" % sum(1 for l in ludics if l <= 1000)) print("\n2000'th..2005'th ludic primes:") print(ludics[2000-1: 2005])
n = 250 triplets = [(x, x+2, x+6) for x in ludics if x+6 < n and x+2 in ludics and x+6 in ludics] print('\nThere are %i triplets less than %i:\n %r' % (len(triplets), n, triplets))`); expect(code.language).toEqual("Python");});
test("gamma function", () => { const code = detectLang(`'''Gamma function'''
def gamma_(tbl): def go(x): return 1.0 / reduce( lambda a, x: a * y + x, tbl[-1] ) return go
TBL = []
def main(): print('hello')
if __name__ == '__main__': main()`); expect(code.language).toEqual("Python");});
test("fivenum", () => { const code = detectLang(`from __future__ import division import math import sys def fivenum(array): n = len(array) if n == 0: print("you entered an empty array.") sys.exit() x = sorted(array) n4 = math.floor((n+3.0)/2.0)/2.0 d = [1, n4, (n+1)/2, n+1-n4, n] sum_array = [] for e in range(5): floor = int(math.floor(d[e] - 1)) ceil = int(math.ceil(d[e] - 1)) sum_array.append(0.5 * (x[floor] + x[ceil])) return sum_array x = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578] y = fivenum(x) print(y)`); expect(code.language).toEqual("Python");});