py2cpp-training / builtin_dataset.json
Ajedrel's picture
Files required to fine tunning
f3e9bca verified
[{"python":"import heapq\n\nheap = []\nfor v in [5,1,3,2,4]:\n heapq.heappush(heap, v)\nwhile heap:\n print(heapq.heappop(heap))","cpp":"#include <iostream>\n#include <queue>\n#include <vector>\nusing namespace std;\n\nint main() {\n priority_queue<int, vector<int>, greater<int>> pq;\n for (int v : {5,1,3,2,4}) pq.push(v);\n while (!pq.empty()) {\n cout << pq.top() << \"\\n\";\n pq.pop();\n }\n return 0;\n}"},{"python":"class Node:\n def __init__(self, val):\n self.val = val\n self.next = None\n\nclass LinkedList:\n def __init__(self):\n self.head = None\n def append(self, val):\n n = Node(val)\n if not self.head: self.head = n; return\n cur = self.head\n while cur.next: cur = cur.next\n cur.next = n\n def print_list(self):\n cur = self.head\n while cur:\n print(cur.val, end=' -> ')\n cur = cur.next\n print('None')\n\nll = LinkedList()\nfor v in [1,2,3,4,5]: ll.append(v)\nll.print_list()","cpp":"#include <iostream>\nusing namespace std;\n\nstruct Node {\n int val;\n Node* next;\n Node(int v) : val(v), next(nullptr) {}\n};\n\nstruct LinkedList {\n Node* head = nullptr;\n void append(int val) {\n Node* n = new Node(val);\n if (!head) { head = n; return; }\n Node* cur = head;\n while (cur->next) cur = cur->next;\n cur->next = n;\n }\n void print_list() {\n for (Node* cur = head; cur; cur = cur->next)\n cout << cur->val << \" -> \";\n cout << \"None\\n\";\n }\n ~LinkedList() {\n while (head) { Node* t = head->next; delete head; head = t; }\n }\n};\n\nint main() {\n LinkedList ll;\n for (int v : {1,2,3,4,5}) ll.append(v);\n ll.print_list();\n return 0;\n}"},{"python":"def has_cycle(graph, n):\n visited = [False] * n\n rec_stack = [False] * n\n def dfs(v):\n visited[v] = rec_stack[v] = True\n for u in graph[v]:\n if not visited[u] and dfs(u): return True\n elif rec_stack[u]: return True\n rec_stack[v] = False\n return False\n return any(not visited[v] and dfs(v) for v in range(n))\ng = [[1],[2],[0],[4],[]]\nprint(has_cycle(g, 5))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\nbool dfs(int v, const vector<vector<int>>& g,\n vector<bool>& vis, vector<bool>& rec) {\n vis[v] = rec[v] = true;\n for (int u : g[v]) {\n if (!vis[u] && dfs(u, g, vis, rec)) return true;\n else if (rec[u]) return true;\n }\n rec[v] = false; return false;\n}\nbool has_cycle(const vector<vector<int>>& g) {\n int n = g.size();\n vector<bool> vis(n,false), rec(n,false);\n for (int i = 0; i < n; i++) if (!vis[i] && dfs(i,g,vis,rec)) return true;\n return false;\n}\nint main() {\n vector<vector<int>> g = {{1},{2},{0},{4},{}};\n cout << (has_cycle(g) ? \"True\" : \"False\") << \"\\n\";\n return 0;\n}"},{"python":"a, b = 10, 20\nprint(max(a, b))\nprint(min(a, b))","cpp":"#include <iostream>\n#include <algorithm>\nusing namespace std;\nint main() {\n int a = 10, b = 20;\n cout << max(a, b) << \"\\n\";\n cout << min(a, b) << \"\\n\";\n return 0;\n}"},{"python":"s = 'banana'\nprint(s.count('a'))\nprint(s.find('nan'))\nprint(s.replace('a', 'o'))","cpp":"#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint main() {\n string s = \"banana\";\n int cnt = 0; size_t pos = 0;\n while ((pos = s.find('a', pos)) != string::npos) { cnt++; pos++; }\n cout << cnt << \"\\n\";\n cout << (int)s.find(\"nan\") << \"\\n\";\n string r = s;\n size_t p = 0;\n while ((p = r.find('a', p)) != string::npos) { r[p] = 'o'; p++; }\n cout << r << \"\\n\";\n return 0;\n}"},{"python":"nums = range(1, 11)\nsquares = list(map(lambda x: x*x, nums))\nprint(squares)","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\nusing namespace std;\n\nint main() {\n vector<int> nums(10);\n iota(nums.begin(), nums.end(), 1);\n vector<int> squares(10);\n transform(nums.begin(), nums.end(), squares.begin(),\n [](int x){ return x*x; });\n for (int x : squares) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"name = input()\nprint(f'Hello, {name}!')","cpp":"#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n string name;\n cin >> name;\n cout << \"Hello, \" << name << \"!\" << endl;\n return 0;\n}"},{"python":"def is_perfect(n):\n return n > 1 and sum(i for i in range(1, n) if n % i == 0) == n\nfor i in range(1, 30):\n if is_perfect(i): print(i)","cpp":"#include <iostream>\nusing namespace std;\nbool is_perfect(int n) {\n if (n <= 1) return false;\n int s = 1;\n for (int i = 2; i * i <= n; i++)\n if (n % i == 0) { s += i; if (i != n/i) s += n/i; }\n return s == n;\n}\nint main() {\n for (int i = 1; i < 30; i++) if (is_perfect(i)) cout << i << \"\\n\";\n return 0;\n}"},{"python":"def abs_val(x):\n return x if x >= 0 else -x\n\nprint(abs_val(-5))\nprint(abs_val(3))","cpp":"#include <iostream>\nusing namespace std;\n\nint abs_val(int x) {\n return x >= 0 ? x : -x;\n}\n\nint main() {\n cout << abs_val(-5) << \"\\n\";\n cout << abs_val(3) << \"\\n\";\n return 0;\n}"},{"python":"def rotate(nums, k):\n n = len(nums)\n k %= n\n nums[:] = nums[-k:] + nums[:-k]\n return nums\nprint(rotate([1,2,3,4,5,6,7], 3))","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nvoid rotate(vector<int>& nums, int k) {\n k %= nums.size();\n reverse(nums.begin(), nums.end());\n reverse(nums.begin(), nums.begin()+k);\n reverse(nums.begin()+k, nums.end());\n}\nint main() {\n vector<int> v = {1,2,3,4,5,6,7};\n rotate(v, 3);\n for (int x : v) cout << x << \" \";\n cout << \"\\n\";\n return 0;\n}"},{"python":"def digital_root(n):\n while n >= 10:\n n = sum(int(d) for d in str(n))\n return n\nprint(digital_root(493))","cpp":"#include <iostream>\nusing namespace std;\nint digital_root(int n) {\n while (n >= 10) {\n int s = 0;\n while (n) { s += n % 10; n /= 10; }\n n = s;\n }\n return n;\n}\nint main() { cout << digital_root(493) << \"\\n\"; return 0; }"},{"python":"def factorial(n):\n if n <= 1:\n return 1\n return n * factorial(n - 1)\n\nprint(factorial(10))","cpp":"#include <iostream>\nusing namespace std;\n\nlong long factorial(int n) {\n if (n <= 1) return 1;\n return n * factorial(n - 1);\n}\n\nint main() {\n cout << factorial(10) << endl;\n return 0;\n}"},{"python":"x = 255\nprint(f'{x:08b}')\nprint(f'{x:04o}')\nprint(f'{x:02x}')","cpp":"#include <iostream>\n#include <bitset>\n#include <iomanip>\nusing namespace std;\nint main() {\n int x = 255;\n cout << bitset<8>(x) << \"\\n\";\n cout << setw(4) << setfill('0') << oct << x << \"\\n\";\n cout << setw(2) << setfill('0') << hex << x << \"\\n\";\n return 0;\n}"},{"python":"a, b = 12, 8\nprint(a & b)\nprint(a | b)\nprint(a ^ b)\nprint(~a)\nprint(a << 2)\nprint(a >> 1)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int a = 12, b = 8;\n cout << (a & b) << \"\\n\" << (a | b) << \"\\n\"\n << (a ^ b) << \"\\n\" << (~a) << \"\\n\"\n << (a << 2) << \"\\n\" << (a >> 1) << \"\\n\";\n return 0;\n}"},{"python":"def collatz(n):\n steps = 0\n while n != 1:\n n = n // 2 if n % 2 == 0 else 3 * n + 1\n steps += 1\n return steps\nprint(collatz(27))","cpp":"#include <iostream>\nusing namespace std;\nint collatz(long long n) {\n int steps = 0;\n while (n != 1) { n = (n % 2 == 0) ? n/2 : 3*n+1; steps++; }\n return steps;\n}\nint main() { cout << collatz(27) << \"\\n\"; return 0; }"},{"python":"def fib_dp(n):\n dp = [0] * (n + 1)\n dp[1] = 1\n for i in range(2, n + 1):\n dp[i] = dp[i-1] + dp[i-2]\n return dp[n]\n\nprint(fib_dp(20))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nlong long fib_dp(int n) {\n vector<long long> dp(n + 1, 0);\n if (n >= 1) dp[1] = 1;\n for (int i = 2; i <= n; i++)\n dp[i] = dp[i-1] + dp[i-2];\n return dp[n];\n}\n\nint main() {\n cout << fib_dp(20) << endl;\n return 0;\n}"},{"python":"class Animal:\n def __init__(self, name, sound):\n self.name = name\n self.sound = sound\n def speak(self):\n print(f'{self.name} says {self.sound}')\n\nclass Dog(Animal):\n def __init__(self, name):\n super().__init__(name, 'Woof')\n def fetch(self):\n print(f'{self.name} fetches the ball!')\n\nd = Dog('Rex')\nd.speak()\nd.fetch()","cpp":"#include <iostream>\n#include <string>\nusing namespace std;\n\nclass Animal {\nprotected:\n string name, sound;\npublic:\n Animal(const string& n, const string& s) : name(n), sound(s) {}\n virtual void speak() {\n cout << name << \" says \" << sound << \"\\n\";\n }\n virtual ~Animal() = default;\n};\n\nclass Dog : public Animal {\npublic:\n Dog(const string& n) : Animal(n, \"Woof\") {}\n void fetch() {\n cout << name << \" fetches the ball!\\n\";\n }\n};\n\nint main() {\n Dog d(\"Rex\");\n d.speak();\n d.fetch();\n return 0;\n}"},{"python":"def permutations(arr):\n if len(arr) <= 1: return [arr[:]]\n result = []\n for i in range(len(arr)):\n arr[0], arr[i] = arr[i], arr[0]\n for p in permutations(arr[1:]):\n result.append([arr[0]] + p)\n arr[0], arr[i] = arr[i], arr[0]\n return result\nfor p in permutations([1,2,3]): print(p)","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nvoid permutations(vector<int>& arr, int start) {\n if (start == (int)arr.size()-1) {\n for (int x : arr) cout << x << \" \"; cout << \"\\n\"; return;\n }\n for (int i = start; i < (int)arr.size(); i++) {\n swap(arr[start], arr[i]);\n permutations(arr, start+1);\n swap(arr[start], arr[i]);\n }\n}\nint main() {\n vector<int> arr = {1,2,3};\n permutations(arr, 0);\n return 0;\n}"},{"python":"def quick_sort(arr):\n if len(arr) <= 1: return arr\n pivot = arr[len(arr)//2]\n left = [x for x in arr if x < pivot]\n mid = [x for x in arr if x == pivot]\n right= [x for x in arr if x > pivot]\n return quick_sort(left) + mid + quick_sort(right)\nprint(quick_sort([3,6,8,10,1,2,1]))","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nvoid quick_sort(vector<int>& arr, int lo, int hi) {\n if (lo >= hi) return;\n int pivot = arr[(lo+hi)/2], i = lo, j = hi;\n while (i <= j) {\n while (arr[i] < pivot) i++;\n while (arr[j] > pivot) j--;\n if (i <= j) { swap(arr[i++], arr[j--]); }\n }\n quick_sort(arr, lo, j);\n quick_sort(arr, i, hi);\n}\nint main() {\n vector<int> arr = {3,6,8,10,1,2,1};\n quick_sort(arr, 0, arr.size()-1);\n for (int x : arr) cout << x << \" \";\n cout << \"\\n\";\n return 0;\n}"},{"python":"def transpose(matrix):\n rows, cols = len(matrix), len(matrix[0])\n return [[matrix[r][c] for r in range(rows)] for c in range(cols)]\n\nm = [[1,2,3],[4,5,6]]\nfor row in transpose(m): print(row)","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<vector<int>> transpose(const vector<vector<int>>& m) {\n int rows = m.size(), cols = m[0].size();\n vector<vector<int>> t(cols, vector<int>(rows));\n for (int i = 0; i < rows; i++)\n for (int j = 0; j < cols; j++)\n t[j][i] = m[i][j];\n return t;\n}\n\nint main() {\n vector<vector<int>> m = {{1,2,3},{4,5,6}};\n auto t = transpose(m);\n for (auto& row : t) {\n for (int x : row) cout << x << \" \";\n cout << \"\\n\";\n }\n return 0;\n}"},{"python":"freq = {}\nwords = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']\nfor w in words:\n freq[w] = freq.get(w, 0) + 1\nprint(freq)","cpp":"#include <iostream>\n#include <unordered_map>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint main() {\n unordered_map<string, int> freq;\n vector<string> words = {\"apple\",\"banana\",\"apple\",\"cherry\",\"banana\",\"apple\"};\n for (auto& w : words) freq[w]++;\n for (auto& [k, v] : freq) cout << k << \": \" << v << \"\\n\";\n return 0;\n}"},{"python":"arr = [1, 2, 3, 4, 5, 6]\nevens = [x for x in arr if x % 2 == 0]\nprint(evens)","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n vector<int> arr = {1, 2, 3, 4, 5, 6};\n vector<int> evens;\n for (int x : arr)\n if (x % 2 == 0) evens.push_back(x);\n for (int x : evens) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"def bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n for j in range(n - i - 1):\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]\n return arr\n\nprint(bubble_sort([64,34,25,12,22,11,90]))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvoid bubble_sort(vector<int>& arr) {\n int n = arr.size();\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n-i-1; j++)\n if (arr[j] > arr[j+1])\n swap(arr[j], arr[j+1]);\n}\n\nint main() {\n vector<int> arr = {64,34,25,12,22,11,90};\n bubble_sort(arr);\n for (int x : arr) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"def memoize(f):\n cache = {}\n def wrapper(*args):\n if args not in cache:\n cache[args] = f(*args)\n return cache[args]\n return wrapper\n\n@memoize\ndef fib(n):\n if n < 2: return n\n return fib(n-1) + fib(n-2)\n\nprint(fib(40))","cpp":"#include <iostream>\n#include <unordered_map>\nusing namespace std;\nunordered_map<int,long long> cache;\nlong long fib(int n) {\n if (n < 2) return n;\n if (cache.count(n)) return cache[n];\n return cache[n] = fib(n-1) + fib(n-2);\n}\nint main() { cout << fib(40) << \"\\n\"; return 0; }"},{"python":"from collections import defaultdict\ngraph = defaultdict(list)\nedges = [(0,1),(0,2),(1,3),(2,3)]\nfor u, v in edges:\n graph[u].append(v)\n graph[v].append(u)\nfor k in sorted(graph):\n print(k, '->', graph[k])","cpp":"#include <iostream>\n#include <vector>\n#include <map>\nusing namespace std;\nint main() {\n map<int, vector<int>> graph;\n vector<pair<int,int>> edges = {{0,1},{0,2},{1,3},{2,3}};\n for (auto [u,v] : edges) { graph[u].push_back(v); graph[v].push_back(u); }\n for (auto& [k, v] : graph) {\n cout << k << \" -> [\";\n for (int i = 0; i < (int)v.size(); i++)\n cout << v[i] << (i+1<(int)v.size()?\", \":\"\");\n cout << \"]\\n\";\n }\n return 0;\n}"},{"python":"class Counter:\n _count = 0\n def __init__(self):\n Counter._count += 1\n self.id = Counter._count\n @classmethod\n def get_count(cls): return cls._count\n def __repr__(self): return f'Counter(id={self.id})'\n\na, b, c = Counter(), Counter(), Counter()\nprint(Counter.get_count())\nprint(a)","cpp":"#include <iostream>\nusing namespace std;\n\nclass Counter {\n static int _count;\n int id;\npublic:\n Counter() : id(++_count) {}\n static int get_count() { return _count; }\n void print() const { cout << \"Counter(id=\" << id << \")\\n\"; }\n};\nint Counter::_count = 0;\n\nint main() {\n Counter a, b, c;\n cout << Counter::get_count() << \"\\n\";\n a.print();\n return 0;\n}"},{"python":"class LRUCache:\n def __init__(self, capacity):\n self.cap = capacity\n self.cache = {}\n self.order = []\n def get(self, key):\n if key not in self.cache: return -1\n self.order.remove(key); self.order.append(key)\n return self.cache[key]\n def put(self, key, val):\n if key in self.cache: self.order.remove(key)\n elif len(self.cache) == self.cap:\n old = self.order.pop(0); del self.cache[old]\n self.cache[key] = val; self.order.append(key)\n\nc = LRUCache(3)\nc.put(1,10); c.put(2,20); c.put(3,30)\nprint(c.get(1))\nc.put(4,40)\nprint(c.get(2))","cpp":"#include <iostream>\n#include <list>\n#include <unordered_map>\nusing namespace std;\nclass LRUCache {\n int cap;\n list<pair<int,int>> lst;\n unordered_map<int, list<pair<int,int>>::iterator> mp;\npublic:\n LRUCache(int c) : cap(c) {}\n int get(int key) {\n if (!mp.count(key)) return -1;\n lst.splice(lst.end(), lst, mp[key]);\n return mp[key]->second;\n }\n void put(int key, int val) {\n if (mp.count(key)) { lst.erase(mp[key]); mp.erase(key); }\n else if ((int)lst.size() == cap) { mp.erase(lst.front().first); lst.pop_front(); }\n lst.push_back({key, val});\n mp[key] = prev(lst.end());\n }\n};\nint main() {\n LRUCache c(3);\n c.put(1,10); c.put(2,20); c.put(3,30);\n cout << c.get(1) << \"\\n\";\n c.put(4,40);\n cout << c.get(2) << \"\\n\";\n return 0;\n}"},{"python":"s = input()\nif s == s[::-1]:\n print('palindrome')\nelse:\n print('not palindrome')","cpp":"#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n string s;\n cin >> s;\n string rev = s;\n reverse(rev.begin(), rev.end());\n cout << (s == rev ? \"palindrome\" : \"not palindrome\") << endl;\n return 0;\n}"},{"python":"def longest_common_subsequence(s1, s2):\n m, n = len(s1), len(s2)\n dp = [[0]*(n+1) for _ in range(m+1)]\n for i in range(1, m+1):\n for j in range(1, n+1):\n if s1[i-1] == s2[j-1]: dp[i][j] = dp[i-1][j-1] + 1\n else: dp[i][j] = max(dp[i-1][j], dp[i][j-1])\n return dp[m][n]\nprint(longest_common_subsequence('ABCBDAB','BDCAB'))","cpp":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint lcs(const string& s1, const string& s2) {\n int m = s1.size(), n = s2.size();\n vector<vector<int>> dp(m+1, vector<int>(n+1, 0));\n for (int i = 1; i <= m; i++)\n for (int j = 1; j <= n; j++)\n dp[i][j] = (s1[i-1]==s2[j-1]) ? dp[i-1][j-1]+1\n : max(dp[i-1][j], dp[i][j-1]);\n return dp[m][n];\n}\nint main() { cout << lcs(\"ABCBDAB\",\"BDCAB\") << \"\\n\"; return 0; }"},{"python":"def selection_sort(arr):\n n = len(arr)\n for i in range(n):\n min_idx = i\n for j in range(i+1, n):\n if arr[j] < arr[min_idx]:\n min_idx = j\n arr[i], arr[min_idx] = arr[min_idx], arr[i]\n return arr\n\nprint(selection_sort([64,25,12,22,11]))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvoid selection_sort(vector<int>& arr) {\n int n = arr.size();\n for (int i = 0; i < n; i++) {\n int min_idx = i;\n for (int j = i+1; j < n; j++)\n if (arr[j] < arr[min_idx]) min_idx = j;\n swap(arr[i], arr[min_idx]);\n }\n}\n\nint main() {\n vector<int> arr = {64,25,12,22,11};\n selection_sort(arr);\n for (int x : arr) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"n = int(input())\ni = 1\nwhile i <= n:\n print(i)\n i *= 2","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n int i = 1;\n while (i <= n) {\n cout << i << \"\\n\";\n i *= 2;\n }\n return 0;\n}"},{"python":"def coin_change(coins, amount):\n dp = [float('inf')] * (amount + 1)\n dp[0] = 0\n for i in range(1, amount + 1):\n for c in coins:\n if c <= i and dp[i - c] + 1 < dp[i]:\n dp[i] = dp[i - c] + 1\n return dp[amount] if dp[amount] != float('inf') else -1\n\nprint(coin_change([1, 5, 10, 25], 36))","cpp":"#include <iostream>\n#include <vector>\n#include <climits>\n#include <algorithm>\nusing namespace std;\n\nint coin_change(const vector<int>& coins, int amount) {\n vector<int> dp(amount + 1, INT_MAX);\n dp[0] = 0;\n for (int i = 1; i <= amount; i++)\n for (int c : coins)\n if (c <= i && dp[i-c] != INT_MAX)\n dp[i] = min(dp[i], dp[i-c] + 1);\n return dp[amount] == INT_MAX ? -1 : dp[amount];\n}\n\nint main() {\n cout << coin_change({1, 5, 10, 25}, 36) << endl;\n return 0;\n}"},{"python":"total = 0\nfor i in range(1, 11):\n if i % 2 != 0:\n total += i\nprint(total)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int total = 0;\n for (int i = 1; i <= 10; i++)\n if (i % 2 != 0) total += i;\n cout << total << \"\\n\";\n return 0;\n}"},{"python":"s = 'hello'\nprint(s.capitalize())\nprint(s.title())\nprint(s.center(11, '*'))","cpp":"#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint main() {\n string s = \"hello\";\n string cap = s; cap[0] = toupper(cap[0]);\n cout << cap << \"\\n\";\n string title = s; title[0] = toupper(title[0]);\n cout << title << \"\\n\";\n int pad = (11 - (int)s.size()) / 2;\n cout << string(pad,'*') << s << string(11-(int)s.size()-pad,'*') << \"\\n\";\n return 0;\n}"},{"python":"def armstrong(n):\n digits = str(n)\n p = len(digits)\n return sum(int(d)**p for d in digits) == n\nfor i in range(100, 1000):\n if armstrong(i): print(i)","cpp":"#include <iostream>\n#include <cmath>\n#include <string>\nusing namespace std;\nbool armstrong(int n) {\n string s = to_string(n);\n int p = s.size(), total = 0;\n for (char c : s) total += (int)pow(c-'0', p);\n return total == n;\n}\nint main() {\n for (int i = 100; i < 1000; i++) if (armstrong(i)) cout << i << \"\\n\";\n return 0;\n}"},{"python":"def dfs(graph, v, visited=None):\n if visited is None: visited = set()\n visited.add(v)\n result = [v]\n for u in graph[v]:\n if u not in visited:\n result += dfs(graph, u, visited)\n return result\n\ng = {0:[1,2], 1:[0,3], 2:[0,4], 3:[1], 4:[2]}\nprint(dfs(g, 0))","cpp":"#include <iostream>\n#include <vector>\n#include <set>\nusing namespace std;\n\nvoid dfs(const vector<vector<int>>& graph, int v,\n set<int>& visited, vector<int>& result) {\n visited.insert(v);\n result.push_back(v);\n for (int u : graph[v])\n if (!visited.count(u))\n dfs(graph, u, visited, result);\n}\n\nint main() {\n vector<vector<int>> g = {{1,2},{0,3},{0,4},{1},{2}};\n set<int> visited;\n vector<int> result;\n dfs(g, 0, visited, result);\n for (int x : result) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"t = int(input())\nfor _ in range(t):\n n = int(input())\n print(n * (n + 1) // 2)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int t; cin >> t;\n while (t--) {\n long long n; cin >> n;\n cout << n*(n+1)/2 << \"\\n\";\n }\n return 0;\n}"},{"python":"from functools import reduce\nnums = [1,2,3,4,5]\nproduct = reduce(lambda a,b: a*b, nums)\nprint(product)","cpp":"#include <iostream>\n#include <vector>\n#include <numeric>\nusing namespace std;\n\nint main() {\n vector<int> nums = {1,2,3,4,5};\n int product = accumulate(nums.begin(), nums.end(), 1,\n [](int a, int b){ return a*b; });\n cout << product << endl;\n return 0;\n}"},{"python":"class BankAccount:\n def __init__(self, owner, balance=0):\n self.owner = owner\n self._balance = balance\n def deposit(self, amount):\n if amount > 0: self._balance += amount\n def withdraw(self, amount):\n if 0 < amount <= self._balance: self._balance -= amount\n else: print('Insufficient funds')\n def __str__(self):\n return f'{self.owner}: ${self._balance:.2f}'\n\nacc = BankAccount('Alice', 1000)\nacc.deposit(500)\nacc.withdraw(200)\nprint(acc)","cpp":"#include <iostream>\n#include <string>\n#include <iomanip>\nusing namespace std;\nclass BankAccount {\n string owner; double balance;\npublic:\n BankAccount(const string& o, double b = 0) : owner(o), balance(b) {}\n void deposit(double a) { if (a > 0) balance += a; }\n void withdraw(double a) {\n if (a > 0 && a <= balance) balance -= a;\n else cout << \"Insufficient funds\\n\";\n }\n void print() const {\n cout << owner << \": $\" << fixed << setprecision(2) << balance << \"\\n\";\n }\n};\nint main() {\n BankAccount acc(\"Alice\", 1000);\n acc.deposit(500); acc.withdraw(200);\n acc.print();\n return 0;\n}"},{"python":"def gcd(a, b):\n while b:\n a, b = b, a % b\n return a\n\ndef lcm(a, b):\n return a * b // gcd(a, b)\n\nprint(gcd(48, 18))\nprint(lcm(12, 18))","cpp":"#include <iostream>\nusing namespace std;\n\nint gcd(int a, int b) {\n while (b) { int t = b; b = a % b; a = t; }\n return a;\n}\n\nint lcm(int a, int b) {\n return a / gcd(a, b) * b;\n}\n\nint main() {\n cout << gcd(48, 18) << \"\\n\";\n cout << lcm(12, 18) << \"\\n\";\n return 0;\n}"},{"python":"n = 3\nmatrix = [[i*n + j for j in range(n)] for i in range(n)]\nfor row in matrix:\n print(' '.join(map(str, row)))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n = 3;\n vector<vector<int>> matrix(n, vector<int>(n));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n matrix[i][j] = i*n + j;\n for (auto& row : matrix) {\n for (int j = 0; j < n; j++)\n cout << row[j] << (j < n-1 ? \" \" : \"\\n\");\n }\n return 0;\n}"},{"python":"a = [1, 2, 3]\nb = [4, 5, 6]\nc = a + b\nprint(c)","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n vector<int> a = {1, 2, 3};\n vector<int> b = {4, 5, 6};\n vector<int> c;\n c.insert(c.end(), a.begin(), a.end());\n c.insert(c.end(), b.begin(), b.end());\n for (int x : c) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"class Stack:\n def __init__(self):\n self._data = []\n def push(self, x): self._data.append(x)\n def pop(self): return self._data.pop()\n def peek(self): return self._data[-1]\n def empty(self): return len(self._data) == 0\n def size(self): return len(self._data)\n\ns = Stack()\nfor v in [1,2,3,4,5]: s.push(v)\nwhile not s.empty(): print(s.pop())","cpp":"#include <iostream>\n#include <stack>\nusing namespace std;\n\nint main() {\n stack<int> s;\n for (int v : {1,2,3,4,5}) s.push(v);\n while (!s.empty()) {\n cout << s.top() << \"\\n\";\n s.pop();\n }\n return 0;\n}"},{"python":"from collections import deque\n\nq = deque()\nfor i in range(5): q.append(i)\nwhile q:\n print(q.popleft())","cpp":"#include <iostream>\n#include <queue>\nusing namespace std;\n\nint main() {\n queue<int> q;\n for (int i = 0; i < 5; i++) q.push(i);\n while (!q.empty()) {\n cout << q.front() << \"\\n\";\n q.pop();\n }\n return 0;\n}"},{"python":"arr = [5, 3, 1, 4, 2]\narr.sort()\nprint(arr)","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n vector<int> arr = {5, 3, 1, 4, 2};\n sort(arr.begin(), arr.end());\n for (int x : arr) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"words = ['apple', 'banana', 'cherry']\nprint(', '.join(words))\nprint(' '.join(reversed(words)))","cpp":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint main() {\n vector<string> words = {\"apple\",\"banana\",\"cherry\"};\n for (int i = 0; i < (int)words.size(); i++)\n cout << words[i] << (i+1 < (int)words.size() ? \", \" : \"\\n\");\n for (int i = words.size()-1; i >= 0; i--)\n cout << words[i] << (i > 0 ? \" \" : \"\\n\");\n return 0;\n}"},{"python":"n = int(input())\ndigit_sum = sum(int(d) for d in str(n))\nprint(digit_sum)","cpp":"#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n int n; cin >> n;\n string s = to_string(n < 0 ? -n : n);\n int total = 0;\n for (char c : s) total += c - '0';\n cout << total << \"\\n\";\n return 0;\n}"},{"python":"def celsius_to_fahrenheit(c):\n return c * 9 / 5 + 32\nfor c in [0, 20, 37, 100]:\n print(f'{c}C = {celsius_to_fahrenheit(c):.1f}F')","cpp":"#include <iostream>\n#include <iomanip>\nusing namespace std;\ndouble celsius_to_fahrenheit(double c) { return c * 9.0 / 5.0 + 32.0; }\nint main() {\n for (double c : {0.0, 20.0, 37.0, 100.0})\n cout << fixed << setprecision(1)\n << c << \"C = \" << celsius_to_fahrenheit(c) << \"F\\n\";\n return 0;\n}"},{"python":"total = 0\nfor i in range(1, 101):\n total += i\nprint(total)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int total = 0;\n for (int i = 1; i <= 100; i++) total += i;\n cout << total << endl;\n return 0;\n}"},{"python":"def merge_sort(arr):\n if len(arr) <= 1: return arr\n mid = len(arr) // 2\n l = merge_sort(arr[:mid])\n r = merge_sort(arr[mid:])\n res, i, j = [], 0, 0\n while i < len(l) and j < len(r):\n if l[i] <= r[j]: res.append(l[i]); i += 1\n else: res.append(r[j]); j += 1\n return res + l[i:] + r[j:]\n\nprint(merge_sort([38,27,43,3,9,82,10]))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<int> merge_sort(vector<int> arr) {\n if (arr.size() <= 1) return arr;\n int mid = arr.size() / 2;\n auto l = merge_sort(vector<int>(arr.begin(), arr.begin()+mid));\n auto r = merge_sort(vector<int>(arr.begin()+mid, arr.end()));\n vector<int> res;\n int i = 0, j = 0;\n while (i < (int)l.size() && j < (int)r.size())\n res.push_back(l[i] <= r[j] ? l[i++] : r[j++]);\n while (i < (int)l.size()) res.push_back(l[i++]);\n while (j < (int)r.size()) res.push_back(r[j++]);\n return res;\n}\n\nint main() {\n vector<int> arr = {38,27,43,3,9,82,10};\n arr = merge_sort(arr);\n for (int x : arr) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"lines = []\nimport sys\nfor line in sys.stdin:\n lines.append(line.strip())\nfor line in reversed(lines):\n print(line)","cpp":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint main() {\n vector<string> lines;\n string line;\n while (getline(cin, line)) lines.push_back(line);\n reverse(lines.begin(), lines.end());\n for (auto& l : lines) cout << l << \"\\n\";\n return 0;\n}"},{"python":"def max_subarray(arr):\n max_sum = curr = arr[0]\n for x in arr[1:]:\n curr = max(x, curr + x)\n max_sum = max(max_sum, curr)\n return max_sum\n\nprint(max_subarray([-2,1,-3,4,-1,2,1,-5,4]))","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint max_subarray(const vector<int>& arr) {\n int max_sum = arr[0], curr = arr[0];\n for (int i = 1; i < (int)arr.size(); i++) {\n curr = max(arr[i], curr + arr[i]);\n max_sum = max(max_sum, curr);\n }\n return max_sum;\n}\n\nint main() {\n vector<int> arr = {-2,1,-3,4,-1,2,1,-5,4};\n cout << max_subarray(arr) << endl;\n return 0;\n}"},{"python":"a, b = int(input()), int(input())\nprint(a + b)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int a, b;\n cin >> a >> b;\n cout << a + b << endl;\n return 0;\n}"},{"python":"data = [1,2,3,4,5,6,7,8,9,10]\nresult = list(filter(lambda x: x % 2 == 0, data))\nprint(result)","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n vector<int> data = {1,2,3,4,5,6,7,8,9,10};\n vector<int> result;\n copy_if(data.begin(), data.end(), back_inserter(result),\n [](int x){ return x % 2 == 0; });\n for (int x : result) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"def clamp(val, lo, hi):\n return max(lo, min(hi, val))\n\nprint(clamp(5, 0, 10))\nprint(clamp(-3, 0, 10))","cpp":"#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint clamp_val(int val, int lo, int hi) {\n return max(lo, min(hi, val));\n}\n\nint main() {\n cout << clamp_val(5, 0, 10) << \"\\n\";\n cout << clamp_val(-3, 0, 10) << \"\\n\";\n return 0;\n}"},{"python":"def mat_mul(A, B):\n n,m,p = len(A), len(B), len(B[0])\n C = [[0]*p for _ in range(n)]\n for i in range(n):\n for k in range(m):\n for j in range(p):\n C[i][j] += A[i][k] * B[k][j]\n return C\n\nA=[[1,2],[3,4]]; B=[[5,6],[7,8]]\nfor row in mat_mul(A,B): print(row)","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<vector<int>> mat_mul(const vector<vector<int>>& A,\n const vector<vector<int>>& B) {\n int n = A.size(), m = B.size(), p = B[0].size();\n vector<vector<int>> C(n, vector<int>(p, 0));\n for (int i = 0; i < n; i++)\n for (int k = 0; k < m; k++)\n for (int j = 0; j < p; j++)\n C[i][j] += A[i][k] * B[k][j];\n return C;\n}\n\nint main() {\n vector<vector<int>> A = {{1,2},{3,4}};\n vector<vector<int>> B = {{5,6},{7,8}};\n auto C = mat_mul(A, B);\n for (auto& row : C) {\n for (int x : row) cout << x << \" \";\n cout << \"\\n\";\n }\n return 0;\n}"},{"python":"def is_power_of_two(n):\n return n > 0 and (n & (n - 1)) == 0\nfor x in [1,2,3,4,7,8,16]:\n print(x, is_power_of_two(x))","cpp":"#include <iostream>\nusing namespace std;\nbool is_power_of_two(int n) { return n > 0 && (n & (n-1)) == 0; }\nint main() {\n for (int x : {1,2,3,4,7,8,16})\n cout << x << \" \" << (is_power_of_two(x) ? \"True\" : \"False\") << \"\\n\";\n return 0;\n}"},{"python":"s = input()\nresult = ''.join(c for c in s if c.isalnum())\nprint(result)","cpp":"#include <iostream>\n#include <string>\n#include <cctype>\nusing namespace std;\nint main() {\n string s; getline(cin, s);\n string result;\n for (char c : s) if (isalnum(c)) result += c;\n cout << result << \"\\n\";\n return 0;\n}"},{"python":"arr = [3, 1, 4, 1, 5, 9, 2, 6, 5]\nunique = list(set(arr))\nunique.sort()\nprint(unique)","cpp":"#include <iostream>\n#include <vector>\n#include <set>\nusing namespace std;\n\nint main() {\n vector<int> arr = {3, 1, 4, 1, 5, 9, 2, 6, 5};\n set<int> s(arr.begin(), arr.end());\n for (int x : s) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"def num_digits(n):\n if n == 0: return 1\n count = 0\n n = abs(n)\n while n: count += 1; n //= 10\n return count\nprint(num_digits(12345))","cpp":"#include <iostream>\n#include <cmath>\nusing namespace std;\nint num_digits(int n) {\n if (n == 0) return 1;\n return (int)log10(abs(n)) + 1;\n}\nint main() { cout << num_digits(12345) << \"\\n\"; return 0; }"},{"python":"s = input()\nprint(len(s))\nprint(s.upper())\nprint(s.lower())","cpp":"#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n string s;\n cin >> s;\n cout << s.size() << \"\\n\";\n string up = s; transform(up.begin(), up.end(), up.begin(), ::toupper);\n string lo = s; transform(lo.begin(), lo.end(), lo.begin(), ::tolower);\n cout << up << \"\\n\" << lo << \"\\n\";\n return 0;\n}"},{"python":"n = int(input())\nif n % 2 == 0:\n print('even')\nelse:\n print('odd')","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n if (n % 2 == 0) cout << \"even\" << endl;\n else cout << \"odd\" << endl;\n return 0;\n}"},{"python":"n = int(input())\nfor i in range(1, n + 1):\n print(i * i)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n for (int i = 1; i <= n; i++) {\n cout << i * i << \"\\n\";\n }\n return 0;\n}"},{"python":"s = input()\nvowels = sum(1 for c in s if c in 'aeiouAEIOU')\nconsonants = sum(1 for c in s if c.isalpha() and c not in 'aeiouAEIOU')\nprint(f'vowels={vowels} consonants={consonants}')","cpp":"#include <iostream>\n#include <string>\n#include <cctype>\nusing namespace std;\nint main() {\n string s; cin >> s;\n string v = \"aeiouAEIOU\";\n int vowels = 0, consonants = 0;\n for (char c : s) {\n if (isalpha(c)) {\n if (v.find(c) != string::npos) vowels++;\n else consonants++;\n }\n }\n cout << \"vowels=\" << vowels << \" consonants=\" << consonants << \"\\n\";\n return 0;\n}"},{"python":"class MinStack:\n def __init__(self):\n self.stack = []\n self.min_stack = []\n def push(self, val):\n self.stack.append(val)\n m = val if not self.min_stack else min(val, self.min_stack[-1])\n self.min_stack.append(m)\n def pop(self):\n self.stack.pop(); self.min_stack.pop()\n def get_min(self): return self.min_stack[-1]\n\ns = MinStack()\nfor v in [5,3,7,2,4]: s.push(v)\nprint(s.get_min())","cpp":"#include <iostream>\n#include <stack>\n#include <algorithm>\nusing namespace std;\nclass MinStack {\n stack<int> stk, min_stk;\npublic:\n void push(int v) {\n stk.push(v);\n min_stk.push(min_stk.empty() ? v : min(v, min_stk.top()));\n }\n void pop() { stk.pop(); min_stk.pop(); }\n int get_min() { return min_stk.top(); }\n};\nint main() {\n MinStack s;\n for (int v : {5,3,7,2,4}) s.push(v);\n cout << s.get_min() << \"\\n\";\n return 0;\n}"},{"python":"arr = [1, 2, 3, 4, 5]\nprint(arr[::-1])","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n vector<int> arr = {1, 2, 3, 4, 5};\n reverse(arr.begin(), arr.end());\n for (int x : arr) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"def flatten(lst):\n result = []\n for item in lst:\n if isinstance(item, list):\n result.extend(flatten(item))\n else:\n result.append(item)\n return result\nprint(flatten([1,[2,[3,4]],5]))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n// En C++ se usa template o estructura fija; versión con nivel 2:\nvoid flatten(const vector<vector<int>>& lst, vector<int>& out) {\n for (auto& row : lst) for (int x : row) out.push_back(x);\n}\nint main() {\n vector<vector<int>> lst = {{1},{2,3,4},{5}};\n vector<int> out;\n flatten(lst, out);\n for (int x : out) cout << x << \" \";\n cout << \"\\n\";\n return 0;\n}"},{"python":"from collections import deque\n\ndef bfs(graph, start):\n visited = set([start])\n queue = deque([start])\n order = []\n while queue:\n v = queue.popleft()\n order.append(v)\n for u in graph[v]:\n if u not in visited:\n visited.add(u)\n queue.append(u)\n return order\n\ng = {0:[1,2], 1:[0,3], 2:[0,4], 3:[1], 4:[2]}\nprint(bfs(g, 0))","cpp":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <set>\nusing namespace std;\n\nvector<int> bfs(const vector<vector<int>>& graph, int start) {\n set<int> visited = {start};\n queue<int> q;\n q.push(start);\n vector<int> order;\n while (!q.empty()) {\n int v = q.front(); q.pop();\n order.push_back(v);\n for (int u : graph[v])\n if (!visited.count(u)) { visited.insert(u); q.push(u); }\n }\n return order;\n}\n\nint main() {\n vector<vector<int>> g = {{1,2},{0,3},{0,4},{1},{2}};\n for (int x : bfs(g, 0)) cout << x << \" \";\n cout << endl;\n return 0;\n}"},{"python":"def dijkstra(graph, start):\n import heapq\n dist = {v: float('inf') for v in graph}\n dist[start] = 0\n pq = [(0, start)]\n while pq:\n d, u = heapq.heappop(pq)\n if d > dist[u]: continue\n for v, w in graph[u]:\n if dist[u] + w < dist[v]:\n dist[v] = dist[u] + w\n heapq.heappush(pq, (dist[v], v))\n return dist\ng = {0:[(1,4),(2,1)],1:[(3,1)],2:[(1,2),(3,5)],3:[]}\nprint(dijkstra(g, 0))","cpp":"#include <iostream>\n#include <vector>\n#include <queue>\n#include <climits>\nusing namespace std;\nvector<int> dijkstra(const vector<vector<pair<int,int>>>& g, int start) {\n int n = g.size();\n vector<int> dist(n, INT_MAX);\n dist[start] = 0;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;\n pq.push({0, start});\n while (!pq.empty()) {\n auto [d, u] = pq.top(); pq.pop();\n if (d > dist[u]) continue;\n for (auto [v, w] : g[u])\n if (dist[u] + w < dist[v]) { dist[v] = dist[u]+w; pq.push({dist[v],v}); }\n }\n return dist;\n}\nint main() {\n vector<vector<pair<int,int>>> g = {{{1,4},{2,1}},{{3,1}},{{1,2},{3,5}},{}};\n for (int d : dijkstra(g, 0)) cout << d << \" \";\n cout << \"\\n\";\n return 0;\n}"},{"python":"n = int(input())\narr = list(map(int, input().split()))\nprint(sum(arr) / len(arr))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n);\n for (int& x : arr) cin >> x;\n double avg = 0;\n for (int x : arr) avg += x;\n cout << avg / n << endl;\n return 0;\n}"},{"python":"print('Hello, World!')","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n cout << \"Hello, World!\" << endl;\n return 0;\n}"},{"python":"class Singleton:\n _instance = None\n def __new__(cls):\n if cls._instance is None:\n cls._instance = super().__new__(cls)\n return cls._instance\n def __init__(self): self.value = 0\n\na = Singleton(); a.value = 42\nb = Singleton()\nprint(b.value)\nprint(a is b)","cpp":"#include <iostream>\nusing namespace std;\nclass Singleton {\n static Singleton* instance;\n int value = 0;\n Singleton() {}\npublic:\n static Singleton* get() {\n if (!instance) instance = new Singleton();\n return instance;\n }\n void set_value(int v) { value = v; }\n int get_value() const { return value; }\n};\nSingleton* Singleton::instance = nullptr;\nint main() {\n auto* a = Singleton::get(); a->set_value(42);\n auto* b = Singleton::get();\n cout << b->get_value() << \"\\n\";\n cout << (a == b ? \"True\" : \"False\") << \"\\n\";\n return 0;\n}"},{"python":"def count_bits(n):\n count = 0\n while n:\n count += n & 1\n n >>= 1\n return count\nprint(count_bits(255))","cpp":"#include <iostream>\n#include <bitset>\nusing namespace std;\nint main() {\n cout << bitset<32>(255).count() << \"\\n\";\n return 0;\n}"},{"python":"def subsets(nums):\n result = [[]]\n for n in nums:\n result += [s + [n] for s in result]\n return result\nfor s in subsets([1,2,3]): print(s)","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\nvoid subsets(const vector<int>& nums, int i, vector<int>& cur) {\n for (int x : cur) cout << x << \" \"; cout << \"\\n\";\n for (int j = i; j < (int)nums.size(); j++) {\n cur.push_back(nums[j]);\n subsets(nums, j+1, cur);\n cur.pop_back();\n }\n}\nint main() {\n vector<int> nums = {1,2,3};\n vector<int> cur;\n subsets(nums, 0, cur);\n return 0;\n}"},{"python":"import math\nprint(math.pi)\nprint(math.e)\nprint(math.sqrt(2))","cpp":"#include <iostream>\n#include <cmath>\nusing namespace std;\nint main() {\n cout << M_PI << \"\\n\" << M_E << \"\\n\" << sqrt(2.0) << \"\\n\";\n return 0;\n}"},{"python":"def longest_increasing_subsequence(arr):\n n = len(arr)\n dp = [1] * n\n for i in range(1, n):\n for j in range(i):\n if arr[j] < arr[i]:\n dp[i] = max(dp[i], dp[j] + 1)\n return max(dp)\nprint(longest_increasing_subsequence([10,9,2,5,3,7,101,18]))","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nint lis(const vector<int>& arr) {\n int n = arr.size();\n vector<int> dp(n, 1);\n for (int i = 1; i < n; i++)\n for (int j = 0; j < i; j++)\n if (arr[j] < arr[i]) dp[i] = max(dp[i], dp[j]+1);\n return *max_element(dp.begin(), dp.end());\n}\nint main() { cout << lis({10,9,2,5,3,7,101,18}) << \"\\n\"; return 0; }"},{"python":"s = input()\nprint(s[::-1])","cpp":"#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint main() {\n string s;\n cin >> s;\n reverse(s.begin(), s.end());\n cout << s << endl;\n return 0;\n}"},{"python":"def sieve(n):\n primes = [True] * (n + 1)\n primes[0] = primes[1] = False\n for i in range(2, int(n**0.5) + 1):\n if primes[i]:\n for j in range(i*i, n+1, i):\n primes[j] = False\n return [i for i in range(n+1) if primes[i]]\nprint(sieve(50))","cpp":"#include <iostream>\n#include <vector>\n#include <cmath>\nusing namespace std;\nvector<int> sieve(int n) {\n vector<bool> p(n+1, true);\n p[0] = p[1] = false;\n for (int i = 2; i <= sqrt(n); i++)\n if (p[i]) for (int j = i*i; j <= n; j += i) p[j] = false;\n vector<int> res;\n for (int i = 0; i <= n; i++) if (p[i]) res.push_back(i);\n return res;\n}\nint main() {\n for (int x : sieve(50)) cout << x << \" \";\n cout << \"\\n\";\n return 0;\n}"},{"python":"class Shape:\n def area(self): return 0\n def perimeter(self): return 0\n\nclass Circle(Shape):\n def __init__(self, r): self.r = r\n def area(self): return 3.14159 * self.r ** 2\n def perimeter(self): return 2 * 3.14159 * self.r\n\nclass Rectangle(Shape):\n def __init__(self, w, h): self.w = w; self.h = h\n def area(self): return self.w * self.h\n def perimeter(self): return 2*(self.w+self.h)\n\nshapes = [Circle(5), Rectangle(4, 6)]\nfor s in shapes:\n print(f'area={s.area():.2f} perimeter={s.perimeter():.2f}')","cpp":"#include <iostream>\n#include <vector>\n#include <memory>\n#include <cmath>\n#include <iomanip>\nusing namespace std;\nstruct Shape {\n virtual double area() const = 0;\n virtual double perimeter() const = 0;\n virtual ~Shape() = default;\n};\nstruct Circle : Shape {\n double r;\n Circle(double r) : r(r) {}\n double area() const override { return M_PI * r * r; }\n double perimeter() const override { return 2 * M_PI * r; }\n};\nstruct Rectangle : Shape {\n double w, h;\n Rectangle(double w, double h) : w(w), h(h) {}\n double area() const override { return w * h; }\n double perimeter() const override { return 2*(w+h); }\n};\nint main() {\n vector<unique_ptr<Shape>> shapes;\n shapes.push_back(make_unique<Circle>(5));\n shapes.push_back(make_unique<Rectangle>(4,6));\n cout << fixed << setprecision(2);\n for (auto& s : shapes)\n cout << \"area=\" << s->area() << \" perimeter=\" << s->perimeter() << \"\\n\";\n return 0;\n}"},{"python":"def two_sum(nums, target):\n seen = {}\n for i, x in enumerate(nums):\n if target - x in seen:\n return [seen[target-x], i]\n seen[x] = i\n return []\nprint(two_sum([2,7,11,15], 9))","cpp":"#include <iostream>\n#include <vector>\n#include <unordered_map>\nusing namespace std;\nvector<int> two_sum(const vector<int>& nums, int target) {\n unordered_map<int,int> seen;\n for (int i = 0; i < (int)nums.size(); i++) {\n if (seen.count(target - nums[i]))\n return {seen[target - nums[i]], i};\n seen[nums[i]] = i;\n }\n return {};\n}\nint main() {\n auto res = two_sum({2,7,11,15}, 9);\n cout << \"[\" << res[0] << \", \" << res[1] << \"]\\n\";\n return 0;\n}"},{"python":"s = ' hello world '\nprint(s.strip())\nprint(s.lstrip())\nprint(s.rstrip())","cpp":"#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\nstring trim(const string& s) {\n auto l = s.find_first_not_of(' ');\n auto r = s.find_last_not_of(' ');\n return (l == string::npos) ? \"\" : s.substr(l, r-l+1);\n}\nint main() {\n string s = \" hello world \";\n cout << trim(s) << \"\\n\";\n auto l = s.find_first_not_of(' ');\n cout << s.substr(l == string::npos ? 0 : l) << \"\\n\";\n auto r = s.find_last_not_of(' ');\n cout << s.substr(0, r == string::npos ? 0 : r+1) << \"\\n\";\n return 0;\n}"},{"python":"d = {'a': 1, 'b': 2, 'c': 3}\nfor k, v in d.items():\n print(f'{k} = {v}')","cpp":"#include <iostream>\n#include <map>\n#include <string>\nusing namespace std;\n\nint main() {\n map<string, int> d = {{\"a\", 1}, {\"b\", 2}, {\"c\", 3}};\n for (auto& [k, v] : d)\n cout << k << \" = \" << v << \"\\n\";\n return 0;\n}"},{"python":"s = 'hello world'\nwords = s.split()\nprint(len(words))\nfor w in words:\n print(w)","cpp":"#include <iostream>\n#include <string>\n#include <sstream>\n#include <vector>\nusing namespace std;\n\nint main() {\n string s = \"hello world\";\n istringstream iss(s);\n vector<string> words;\n string word;\n while (iss >> word) words.push_back(word);\n cout << words.size() << \"\\n\";\n for (auto& w : words) cout << w << \"\\n\";\n return 0;\n}"},{"python":"def edit_distance(s1, s2):\n m, n = len(s1), len(s2)\n dp = [[0]*(n+1) for _ in range(m+1)]\n for i in range(m+1): dp[i][0] = i\n for j in range(n+1): dp[0][j] = j\n for i in range(1,m+1):\n for j in range(1,n+1):\n if s1[i-1]==s2[j-1]: dp[i][j]=dp[i-1][j-1]\n else: dp[i][j]=1+min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])\n return dp[m][n]\nprint(edit_distance('kitten','sitting'))","cpp":"#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint edit_distance(const string& s1, const string& s2) {\n int m = s1.size(), n = s2.size();\n vector<vector<int>> dp(m+1, vector<int>(n+1));\n for (int i = 0; i <= m; i++) dp[i][0] = i;\n for (int j = 0; j <= n; j++) dp[0][j] = j;\n for (int i = 1; i <= m; i++)\n for (int j = 1; j <= n; j++)\n dp[i][j] = (s1[i-1]==s2[j-1]) ? dp[i-1][j-1]\n : 1 + min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]});\n return dp[m][n];\n}\nint main() { cout << edit_distance(\"kitten\",\"sitting\") << \"\\n\"; return 0; }"},{"python":"n = int(input())\nmatrix = []\nfor _ in range(n):\n row = list(map(int, input().split()))\n matrix.append(row)\nfor row in matrix:\n print(*row)","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\nint main() {\n int n; cin >> n;\n vector<vector<int>> matrix(n);\n for (int i = 0; i < n; i++) {\n matrix[i].resize(n);\n for (int& x : matrix[i]) cin >> x;\n }\n for (auto& row : matrix) {\n for (int j = 0; j < (int)row.size(); j++)\n cout << row[j] << (j+1<(int)row.size()?\" \":\"\\n\");\n }\n return 0;\n}"},{"python":"x = float(input())\nprint(f'{x:.2f}')","cpp":"#include <iostream>\n#include <iomanip>\nusing namespace std;\nint main() {\n double x;\n cin >> x;\n cout << fixed << setprecision(2) << x << endl;\n return 0;\n}"},{"python":"n = int(input())\nprint(bin(n))\nprint(oct(n))\nprint(hex(n))","cpp":"#include <iostream>\n#include <bitset>\nusing namespace std;\nint main() {\n int n; cin >> n;\n cout << \"0b\"; for(int i=31;i>=0;i--) if(n>>i&1||i==0) cout<<((n>>i)&1);\n cout << \"\\n0\" << oct << n << \"\\n0x\" << hex << n << \"\\n\";\n return 0;\n}"},{"python":"def insertion_sort(arr):\n for i in range(1, len(arr)):\n key = arr[i]\n j = i - 1\n while j >= 0 and arr[j] > key:\n arr[j+1] = arr[j]\n j -= 1\n arr[j+1] = key\n return arr\nprint(insertion_sort([12,11,13,5,6]))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\nvoid insertion_sort(vector<int>& arr) {\n for (int i = 1; i < (int)arr.size(); i++) {\n int key = arr[i], j = i-1;\n while (j >= 0 && arr[j] > key) { arr[j+1] = arr[j]; j--; }\n arr[j+1] = key;\n }\n}\nint main() {\n vector<int> arr = {12,11,13,5,6};\n insertion_sort(arr);\n for (int x : arr) cout << x << \" \";\n cout << \"\\n\";\n return 0;\n}"},{"python":"def fibonacci(n):\n if n <= 1:\n return n\n return fibonacci(n-1) + fibonacci(n-2)\n\nfor i in range(10):\n print(fibonacci(i))","cpp":"#include <iostream>\nusing namespace std;\n\nint fibonacci(int n) {\n if (n <= 1) return n;\n return fibonacci(n-1) + fibonacci(n-2);\n}\n\nint main() {\n for (int i = 0; i < 10; i++) {\n cout << fibonacci(i) << \"\\n\";\n }\n return 0;\n}"},{"python":"s = 'hello world'\nwords = s.split()\nwords.sort()\nprint(' '.join(words))","cpp":"#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\nint main() {\n string s = \"hello world\";\n istringstream iss(s);\n vector<string> words;\n string w;\n while (iss >> w) words.push_back(w);\n sort(words.begin(), words.end());\n for (int i = 0; i < (int)words.size(); i++)\n cout << words[i] << (i+1 < (int)words.size() ? \" \" : \"\\n\");\n return 0;\n}"},{"python":"def knapsack(weights, values, capacity):\n n = len(weights)\n dp = [[0]*(capacity+1) for _ in range(n+1)]\n for i in range(1, n+1):\n for w in range(capacity+1):\n dp[i][w] = dp[i-1][w]\n if weights[i-1] <= w:\n dp[i][w] = max(dp[i][w], dp[i-1][w-weights[i-1]] + values[i-1])\n return dp[n][capacity]\nprint(knapsack([2,3,4,5],[3,4,5,6],8))","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\nint knapsack(const vector<int>& w, const vector<int>& v, int cap) {\n int n = w.size();\n vector<vector<int>> dp(n+1, vector<int>(cap+1, 0));\n for (int i = 1; i <= n; i++)\n for (int c = 0; c <= cap; c++) {\n dp[i][c] = dp[i-1][c];\n if (w[i-1] <= c) dp[i][c] = max(dp[i][c], dp[i-1][c-w[i-1]]+v[i-1]);\n }\n return dp[n][cap];\n}\nint main() {\n cout << knapsack({2,3,4,5},{3,4,5,6},8) << \"\\n\";\n return 0;\n}"},{"python":"def is_prime(n):\n if n < 2: return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0: return False\n return True\n\nfor i in range(2, 30):\n if is_prime(i): print(i, end=' ')","cpp":"#include <iostream>\n#include <cmath>\nusing namespace std;\n\nbool is_prime(int n) {\n if (n < 2) return false;\n for (int i = 2; i <= sqrt(n); i++)\n if (n % i == 0) return false;\n return true;\n}\n\nint main() {\n for (int i = 2; i < 30; i++)\n if (is_prime(i)) cout << i << \" \";\n cout << endl;\n return 0;\n}"},{"python":"arr = [3, 1, 4, 1, 5, 9, 2, 6]\nprint(sum(arr))\nprint(min(arr))\nprint(max(arr))","cpp":"#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <numeric>\nusing namespace std;\n\nint main() {\n vector<int> arr = {3, 1, 4, 1, 5, 9, 2, 6};\n cout << accumulate(arr.begin(), arr.end(), 0) << \"\\n\";\n cout << *min_element(arr.begin(), arr.end()) << \"\\n\";\n cout << *max_element(arr.begin(), arr.end()) << \"\\n\";\n return 0;\n}"},{"python":"s = input()\ncount = {}\nfor c in s:\n count[c] = count.get(c, 0) + 1\nfor c, n in sorted(count.items()):\n print(f'{c}: {n}')","cpp":"#include <iostream>\n#include <string>\n#include <map>\nusing namespace std;\n\nint main() {\n string s;\n cin >> s;\n map<char, int> count;\n for (char c : s) count[c]++;\n for (auto& [c, n] : count)\n cout << c << \": \" << n << \"\\n\";\n return 0;\n}"},{"python":"d = {'x': 10, 'y': 20}\nif 'x' in d:\n print(d['x'])\nd['z'] = 30\nprint(len(d))","cpp":"#include <iostream>\n#include <unordered_map>\n#include <string>\nusing namespace std;\n\nint main() {\n unordered_map<string, int> d = {{\"x\", 10}, {\"y\", 20}};\n if (d.count(\"x\")) cout << d[\"x\"] << \"\\n\";\n d[\"z\"] = 30;\n cout << d.size() << \"\\n\";\n return 0;\n}"},{"python":"def binary_search(arr, target):\n lo, hi = 0, len(arr) - 1\n while lo <= hi:\n mid = (lo + hi) // 2\n if arr[mid] == target: return mid\n elif arr[mid] < target: lo = mid + 1\n else: hi = mid - 1\n return -1\n\narr = [1,3,5,7,9,11,13]\nprint(binary_search(arr, 7))","cpp":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nint binary_search(const vector<int>& arr, int target) {\n int lo = 0, hi = arr.size() - 1;\n while (lo <= hi) {\n int mid = (lo + hi) / 2;\n if (arr[mid] == target) return mid;\n else if (arr[mid] < target) lo = mid + 1;\n else hi = mid - 1;\n }\n return -1;\n}\n\nint main() {\n vector<int> arr = {1,3,5,7,9,11,13};\n cout << binary_search(arr, 7) << endl;\n return 0;\n}"},{"python":"def n_queens(n):\n solutions = []\n def solve(row, cols, diag1, diag2, board):\n if row == n: solutions.append(board[:]); return\n for col in range(n):\n if col in cols or row-col in diag1 or row+col in diag2: continue\n cols.add(col); diag1.add(row-col); diag2.add(row+col)\n board.append(col)\n solve(row+1, cols, diag1, diag2, board)\n board.pop(); cols.remove(col); diag1.remove(row-col); diag2.remove(row+col)\n solve(0,set(),set(),set(),[])\n return len(solutions)\nprint(n_queens(8))","cpp":"#include <iostream>\n#include <vector>\n#include <set>\nusing namespace std;\nint count = 0;\nvoid solve(int row, int n, set<int>& cols, set<int>& d1, set<int>& d2) {\n if (row == n) { count++; return; }\n for (int col = 0; col < n; col++) {\n if (cols.count(col)||d1.count(row-col)||d2.count(row+col)) continue;\n cols.insert(col); d1.insert(row-col); d2.insert(row+col);\n solve(row+1, n, cols, d1, d2);\n cols.erase(col); d1.erase(row-col); d2.erase(row+col);\n }\n}\nint main() {\n set<int> c, d1, d2; solve(0, 8, c, d1, d2);\n cout << count << \"\\n\";\n return 0;\n}"},{"python":"n = int(input())\nfor i in range(n):\n print(i)","cpp":"#include <iostream>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n for (int i = 0; i < n; i++) {\n cout << i << \"\\n\";\n }\n return 0;\n}"},{"python":"def power(base, exp):\n result = 1\n for _ in range(exp):\n result *= base\n return result\n\nprint(power(2, 10))","cpp":"#include <iostream>\nusing namespace std;\n\nlong long power(long long base, int exp) {\n long long result = 1;\n for (int i = 0; i < exp; i++) result *= base;\n return result;\n}\n\nint main() {\n cout << power(2, 10) << endl;\n return 0;\n}"},{"python":"from collections import Counter\nwords = 'the quick brown fox jumps over the lazy dog the'.split()\ncnt = Counter(words)\nprint(cnt.most_common(3))","cpp":"#include <iostream>\n#include <sstream>\n#include <vector>\n#include <string>\n#include <unordered_map>\n#include <algorithm>\nusing namespace std;\nint main() {\n string text = \"the quick brown fox jumps over the lazy dog the\";\n istringstream iss(text);\n unordered_map<string,int> cnt;\n string w; while (iss >> w) cnt[w]++;\n vector<pair<int,string>> v;\n for (auto& [word, freq] : cnt) v.push_back({freq, word});\n sort(v.rbegin(), v.rend());\n for (int i = 0; i < min(3,(int)v.size()); i++)\n cout << \"(\" << v[i].second << \", \" << v[i].first << \")\\n\";\n return 0;\n}"}]