language stringclasses 3
values | prompt stringclasses 3
values | code stringclasses 3
values | explanation stringclasses 3
values |
|---|---|---|---|
python | Write a Python function to reverse a string. | def reverse_string(s):
return s[::-1] | This function uses slicing. |
javascript | Check if a number is prime. | function isPrime(n) {
for (let i = 2; i < n; i++) {
if (n % i === 0) return false;
}
return n > 1;
} | This loops from 2 to n-1 to test divisibility. |
java | Binary search algorithm. | int binarySearch(int[] arr, int target) {
int l=0,r=arr.length-1;
while(l<=r){ int m=(l+r)/2; if(arr[m]==target)return m; else if(arr[m]<target)l=m+1; else r=m-1; }
return -1;
} | This implements standard binary search logic in Java. |
No dataset card yet
- Downloads last month
- 13