common_id
stringlengths 5
5
| image
stringlengths 18
18
| code
stringlengths 55
1.44k
|
---|---|---|
10503 | Test/png/10503.png | def find_matching(cls, path, patterns):
for pattern in patterns:
if pattern.match(path):
yield pattern
|
11168 | Test/png/11168.png | # Write a python function to get the first element of each sublist.
def Extract(lst):
return [item[0] for item in lst]
|
11189 | Test/png/11189.png | # Write a function to determine if there is a subset of the given set with sum equal to the given sum.
def is_subset_sum(set, n, sum):
if sum == 0:
return True
if n == 0:
return False
if set[n - 1] > sum:
return is_subset_sum(set, n - 1, sum)
return is_subset_sum(set, n - 1, sum) or is_subset_sum(set, n - 1, sum - set[n - 1])
|
10473 | Test/png/10473.png | def disconnect(self):
for name, connection in self.items():
if not connection.is_closed():
connection.close()
|
10855 | Test/png/10855.png | # Write a function to find the maximum total path sum in the given triangle.
def max_path_sum(tri, m, n):
for i in range(m - 1, -1, -1):
for j in range(i + 1):
if tri[i + 1][j] > tri[i + 1][j + 1]:
tri[i][j] += tri[i + 1][j]
else:
tri[i][j] += tri[i + 1][j + 1]
return tri[0][0]
|
10564 | Test/png/10564.png | def get_image(self, obj):
if self._meta.image_field:
return getattr(obj, self._meta.image_field)
|
11081 | Test/png/11081.png | # Write a function to find the volume of a cuboid.
def volume_cuboid(l, w, h):
volume = l * w * h
return volume
|
10590 | Test/png/10590.png | def expected_utility(a, s, U, mdp):
"The expected utility of doing a in state s, according to the MDP and U."
return sum([p * U[s1] for (p, s1) in mdp.T(s, a)])
|
11211 | Test/png/11211.png | # Write a function to add consecutive numbers of a given list.
def add_consecutive_nums(nums):
result = [b + a for a, b in zip(nums[:-1], nums[1:])]
return result
|
11282 | Test/png/11282.png | # Write a function to find the surface area of a cylinder.
def surfacearea_cylinder(r, h):
surfacearea = (2 * 3.1415 * r * r) + (2 * 3.1415 * r * h)
return surfacearea
|
11143 | Test/png/11143.png | # Write a python function to find the last digit of a given number.
def last_Digit(n):
return n % 10
|
10884 | Test/png/10884.png | # Write a function to find the perimeter of a triangle.
def perimeter_triangle(a, b, c):
perimeter = a + b + c
return perimeter
|
10642 | Test/png/10642.png | def get_power(self):
power = (yield from self.handle_int(self.API.get('power')))
return bool(power)
|
10731 | Test/png/10731.png | # Write a python function to find the maximum sum of elements of list in a list of lists.
def maximum_Sum(list1):
maxi = -100000
for x in list1:
sum = 0
for y in x:
sum += y
maxi = max(sum, maxi)
return maxi
|
11016 | Test/png/11016.png | # Write a function to find the specified number of largest products from two given lists.
def large_product(nums1, nums2, N):
result = sorted([x * y for x in nums1 for y in nums2], reverse=True)[:N]
return result
|
11230 | Test/png/11230.png | # Write a function to find the longest bitonic subsequence for the given array.
def lbs(arr):
n = len(arr)
lis = [1 for i in range(n + 1)]
for i in range(1, n):
for j in range(0, i):
if (arr[i] > arr[j]) and (lis[i] < lis[j] + 1):
lis[i] = lis[j] + 1
lds = [1 for i in range(n + 1)]
for i in reversed(range(n - 1)):
for j in reversed(range(i - 1, n)):
if arr[i] > arr[j] and lds[i] < lds[j] + 1:
lds[i] = lds[j] + 1
maximum = lis[0] + lds[0] - 1
for i in range(1, n):
maximum = max((lis[i] + lds[i] - 1), maximum)
return maximum
|
10386 | Test/png/10386.png | def from_json_list(cls, api_client, data):
return [cls.from_json(api_client, item) for item in data]
|
10134 | Test/png/10134.png | def md_to_text(content):
text = None
html = markdown.markdown(content)
if html:
text = html_to_text(content)
return text
|
10656 | Test/png/10656.png | def log_state(entity, state):
p = {'on': entity, 'state': state}
_log(TYPE_CODES.STATE, p)
|
10434 | Test/png/10434.png | def close(self):
self.process.stdout.close()
self.process.stderr.close()
self.running = False
|
10291 | Test/png/10291.png | def last_modified_version(self, **kwargs):
self.items(**kwargs)
return int(self.request.headers.get("last-modified-version", 0))
|
11164 | Test/png/11164.png | # Write a function to reverse strings in a given list of string values.
def reverse_string_list(stringlist):
result = [x[::-1] for x in stringlist]
return result
|
11194 | Test/png/11194.png | # Write a function to compute binomial probability for the given number.
def nCr(n, r):
if r > n / 2:
r = n - r
answer = 1
for i in range(1, r + 1):
answer *= n - r + i
answer /= i
return answer
def binomial_probability(n, k, p):
return nCr(n, k) * pow(p, k) * pow(1 - p, n - k)
|
10554 | Test/png/10554.png | def keys(self):
return self.options.keys() + [p.name for p in self.positional_args]
|
11135 | Test/png/11135.png | # Write a function to convert a date of yyyy-mm-dd format to dd-mm-yyyy format by using regex.
import re
def change_date_format(dt):
return re.sub(r"(\d{4})-(\d{1,2})-(\d{1,2})", "\\3-\\2-\\1", dt)
|
10606 | Test/png/10606.png | def set_netmask(self, netmask):
self.set(ip=self._ip, netmask=netmask)
|
10597 | Test/png/10597.png | def score(self):
"The total score for the words found, according to the rules."
return sum([self.scores[len(w)] for w in self.words()])
|
10828 | Test/png/10828.png | # Write a function to find the maximum product from the pairs of tuples within a given list.
def max_product_tuple(list1):
result_max = max([abs(x * y) for x, y in list1])
return result_max
|
11287 | Test/png/11287.png | # Write a function to find the dissimilar elements in the given two tuples.
def find_dissimilar(test_tup1, test_tup2):
res = tuple(set(test_tup1) ^ set(test_tup2))
return res
|
10110 | Test/png/10110.png | def _append_jpg(self, jpg, before_prompt=False):
self._append_custom(self._insert_jpg, jpg, before_prompt)
|
10635 | Test/png/10635.png | def axes(self):
return [np.array(self.ode_obj.getAxis(i))
for i in range(self.ADOF or self.LDOF)]
|
10550 | Test/png/10550.png | def _tz(self, z):
return (z-self.param_dict['psf-zslab'])*self.param_dict[self.zscale]
|
10830 | Test/png/10830.png | # Write a function to find n’th smart number.
MAX = 3000
def smartNumber(n):
primes = [0] * MAX
result = []
for i in range(2, MAX):
if primes[i] == 0:
primes[i] = 1
j = i * 2
while j < MAX:
primes[j] -= 1
if (primes[j] + 3) == 0:
result.append(j)
j = j + i
result.sort()
return result[n - 1]
|
10557 | Test/png/10557.png | def cp(resume, quiet, dataset_uri, dest_base_uri):
_copy(resume, quiet, dataset_uri, dest_base_uri)
|
10533 | Test/png/10533.png | def validate(cls, state):
return state in [cls.ACTIVE, cls.PENDING_ADMIN, cls.PENDING_USER]
|
10325 | Test/png/10325.png | def next(self, length):
return Segment(self.strip, length, self.offset + self.length)
|
10483 | Test/png/10483.png | def exit(self):
if self._engine:
self._engine.repl.terminate()
self._engine = None
|
10347 | Test/png/10347.png | def _edgeLabel(self, node, parent):
return self.word[node.idx + parent.depth: node.idx + node.depth]
|
10231 | Test/png/10231.png | def rm(self, key):
path = os.path.join(self.uri, key)
os.remove(path)
|
10233 | Test/png/10233.png | def _top(self, n=0):
if len(self.stack) - n < 0:
raise StackUnderflow()
return self.stack[n - 1]
|
10632 | Test/png/10632.png | def position_rates(self):
return [self.ode_obj.getPositionRate(i) for i in range(self.LDOF)]
|
10834 | Test/png/10834.png | # Write a python function to find the sum of common divisors of two given numbers.
def sum(a, b):
sum = 0
for i in range(1, min(a, b)):
if a % i == 0 and b % i == 0:
sum += i
return sum
|
10618 | Test/png/10618.png | def get(self, id, **kwargs):
return super(DomainRecords, self).get(id, **kwargs)
|
11052 | Test/png/11052.png | # Write a python function to find number of elements with odd factors in a given range.
def count_Odd_Squares(n, m):
return int(m**0.5) - int((n - 1) ** 0.5)
|
10882 | Test/png/10882.png | # Write a function to group a sequence of key-value pairs into a dictionary of lists.
def group_keyvalue(l):
result = {}
for k, v in l:
result.setdefault(k, []).append(v)
return result
|
11275 | Test/png/11275.png | # Write a function to check whether a specified list is sorted or not.
def issort_list(list1):
result = all(list1[i] <= list1[i + 1] for i in range(len(list1) - 1))
return result
|
10715 | Test/png/10715.png | # Write a function to find all words which are at least 4 characters long in a string by using regex.
import re
def find_char_long(text):
return re.findall(r"\b\w{4,}\b", text)
|
11097 | Test/png/11097.png | # Write a function to find the n'th lucas number.
def find_lucas(n):
if n == 0:
return 2
if n == 1:
return 1
return find_lucas(n - 1) + find_lucas(n - 2)
|
10850 | Test/png/10850.png | # Write a function to count the same pair in three given lists.
def count_samepair(list1, list2, list3):
result = sum(m == n == o for m, n, o in zip(list1, list2, list3))
return result
|
10616 | Test/png/10616.png | def get(self, id):
info = super(Images, self).get(id)
return ImageActions(self.api, parent=self, **info)
|
10738 | Test/png/10738.png | # Write a python function to count all the substrings starting and ending with same characters.
def check_Equality(s):
return ord(s[0]) == ord(s[len(s) - 1])
def count_Substring_With_Equal_Ends(s):
result = 0
n = len(s)
for i in range(n):
for j in range(1, n - i + 1):
if check_Equality(s[i : i + j]):
result += 1
return result
|
11068 | Test/png/11068.png | # Write a function to find the n’th carol number.
def get_carol(n):
result = (2**n) - 1
return result * result - 2
|
10331 | Test/png/10331.png | def reset_counter(self):
self._cnt_retries = 0
for i in self._url_counter:
self._url_counter[i] = 0
|
10615 | Test/png/10615.png | def datapackage_exists(repo):
datapath = os.path.join(repo.rootdir, "datapackage.json")
return os.path.exists(datapath)
|
11215 | Test/png/11215.png | # Write a function to remove specific words from a given list.
def remove_words(list1, removewords):
for word in list(list1):
if word in removewords:
list1.remove(word)
return list1
|
11019 | Test/png/11019.png | # Write a python function to set the left most unset bit.
def set_left_most_unset_bit(n):
if not (n & (n + 1)):
return n
pos, temp, count = 0, n, 0
while temp:
if not (temp & 1):
pos = count
count += 1
temp >>= 1
return n | (1 << (pos))
|
10870 | Test/png/10870.png | # Write a function to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0).
def sum_series(n):
if n < 1:
return 0
else:
return n + sum_series(n - 2)
|
10789 | Test/png/10789.png | # Write a function to zip the two given tuples.
def zip_tuples(test_tup1, test_tup2):
res = []
for i, j in enumerate(test_tup1):
res.append((j, test_tup2[i % len(test_tup2)]))
return res
|
11072 | Test/png/11072.png | # Write a function to find the number of flips required to make the given binary string a sequence of alternate characters.
def make_flip(ch):
return "1" if (ch == "0") else "0"
def get_flip_with_starting_charcter(str, expected):
flip_count = 0
for i in range(len(str)):
if str[i] != expected:
flip_count += 1
expected = make_flip(expected)
return flip_count
def min_flip_to_make_string_alternate(str):
return min(
get_flip_with_starting_charcter(str, "0"),
get_flip_with_starting_charcter(str, "1"),
)
|
10239 | Test/png/10239.png | def RETURN(self, offset, size):
data = self.read_buffer(offset, size)
raise EndTx('RETURN', data)
|
10762 | Test/png/10762.png | # Write a function to sort the given array by using counting sort.
def counting_sort(my_list):
max_value = 0
for i in range(len(my_list)):
if my_list[i] > max_value:
max_value = my_list[i]
buckets = [0] * (max_value + 1)
for i in my_list:
buckets[i] += 1
i = 0
for j in range(max_value + 1):
for a in range(buckets[j]):
my_list[i] = j
i += 1
return my_list
|
10704 | Test/png/10704.png | def log(self, string):
self.wfile.write(json.dumps({'log': string}) + NEWLINE)
|
10636 | Test/png/10636.png | def axes(self):
return [np.array(self.ode_obj.getAxis1()),
np.array(self.ode_obj.getAxis2())]
|
10698 | Test/png/10698.png | def get_branches(self):
return [self._sanitize(branch)
for branch in self._git.branch(color="never").splitlines()]
|
10761 | Test/png/10761.png | # Write a python function to check whether the first and last characters of a given string are equal or not.
def check_Equality(str):
if str[0] == str[-1]:
return "Equal"
else:
return "Not Equal"
|
10404 | Test/png/10404.png | def down(self):
self.swap(self.get_ordering_queryset().filter(order__gt=self.order))
|
11261 | Test/png/11261.png | # Write a function to convert the given tuple to a floating-point number.
def tuple_to_float(test_tup):
res = float(".".join(str(ele) for ele in test_tup))
return res
|
10938 | Test/png/10938.png | # Write a function to replace blank spaces with any character in a string.
def replace_blank(str1, char):
str2 = str1.replace(" ", char)
return str2
|
11007 | Test/png/11007.png | # Write a function to calculate the maximum aggregate from the list of tuples.
from collections import defaultdict
def max_aggregate(stdata):
temp = defaultdict(int)
for name, marks in stdata:
temp[name] += marks
return max(temp.items(), key=lambda x: x[1])
|
10714 | Test/png/10714.png | # Write a python function to check whether the two numbers differ at one bit position only or not.
def is_Power_Of_Two(x):
return x and (not (x & (x - 1)))
def differ_At_One_Bit_Pos(a, b):
return is_Power_Of_Two(a ^ b)
|
10400 | Test/png/10400.png | def set_debug():
logging.basicConfig(level=logging.WARNING)
peony.logger.setLevel(logging.DEBUG)
|
10107 | Test/png/10107.png | def write(self, filename):
txt = self.tostring()
with open(filename, 'w') as f:
f.write(txt)
|
11093 | Test/png/11093.png | # Write a function to find the n'th perrin number using recursion.
def get_perrin(n):
if n == 0:
return 3
if n == 1:
return 0
if n == 2:
return 2
return get_perrin(n - 2) + get_perrin(n - 3)
|
10937 | Test/png/10937.png | # Write a function to re-arrange the elements of the given array so that all negative elements appear before positive ones.
def re_arrange_array(arr, n):
j = 0
for i in range(0, n):
if arr[i] < 0:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
j = j + 1
return arr
|
11075 | Test/png/11075.png | # Write a function to check if a binary tree is balanced or not.
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def get_height(root):
if root is None:
return 0
return max(get_height(root.left), get_height(root.right)) + 1
def is_tree_balanced(root):
if root is None:
return True
lh = get_height(root.left)
rh = get_height(root.right)
if (
(abs(lh - rh) <= 1)
and is_tree_balanced(root.left) is True
and is_tree_balanced(root.right) is True
):
return True
return False
|
10427 | Test/png/10427.png | def _mouse_pointer_moved(self, x, y):
self._namespace['MOUSEX'] = x
self._namespace['MOUSEY'] = y
|
11058 | Test/png/11058.png | # Write a python function to minimize the length of the string by removing occurrence of only one character.
def minimum_Length(s):
maxOcc = 0
n = len(s)
arr = [0] * 26
for i in range(n):
arr[ord(s[i]) - ord("a")] += 1
for i in range(26):
if arr[i] > maxOcc:
maxOcc = arr[i]
return n - maxOcc
|
10627 | Test/png/10627.png | def read_xl(xl_path: str):
xl_path, choice = _check_xl_path(xl_path)
reader = XL_READERS[choice]
return reader(xl_path)
|
10106 | Test/png/10106.png | def select_right(self):
r, c = self._index
self._select_index(r, c+1)
|
11051 | Test/png/11051.png | # Write a function to calculate the number of digits and letters in a string.
def dig_let(s):
d = l = 0
for c in s:
if c.isdigit():
d = d + 1
elif c.isalpha():
l = l + 1
else:
pass
return (l, d)
|
11204 | Test/png/11204.png | # Write a function to find the smallest integers from a given list of numbers using heap queue algorithm.
import heapq as hq
def heap_queue_smallest(nums, n):
smallest_nums = hq.nsmallest(n, nums)
return smallest_nums
|
11124 | Test/png/11124.png | # Write a function to find the maximum sum we can make by dividing number in three parts recursively and summing them up together for the given number.
MAX = 1000000
def breakSum(n):
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = 1
for i in range(2, n + 1):
dp[i] = max(dp[int(i / 2)] + dp[int(i / 3)] + dp[int(i / 4)], i)
return dp[n]
|
11278 | Test/png/11278.png | # Write a function to remove words from a given list of strings containing a character or string.
def remove_words(list1, charlist):
new_list = []
for line in list1:
new_words = " ".join(
[
word
for word in line.split()
if not any([phrase in word for phrase in charlist])
]
)
new_list.append(new_words)
return new_list
|
10677 | Test/png/10677.png | def is_home_environment(path):
home = unipath(os.environ.get('CPENV_HOME', '~/.cpenv'))
path = unipath(path)
return path.startswith(home)
|
10273 | Test/png/10273.png | def locked_delete(self):
query = {self.key_name: self.key_value}
self.model_class.objects.filter(**query).delete()
|
10204 | Test/png/10204.png | def mouse_event(dwFlags: int, dx: int, dy: int, dwData: int, dwExtraInfo: int) -> None:
ctypes.windll.user32.mouse_event(dwFlags, dx, dy, dwData, dwExtraInfo)
|
10883 | Test/png/10883.png | # Write a function to verify validity of a string of parentheses.
def is_valid_parenthese(str1):
stack, pchar = [], {"(": ")", "{": "}", "[": "]"}
for parenthese in str1:
if parenthese in pchar:
stack.append(parenthese)
elif len(stack) == 0 or pchar[stack.pop()] != parenthese:
return False
return len(stack) == 0
|
10962 | Test/png/10962.png | # Write a function to find all words starting with 'a' or 'e' in a given string.
import re
def words_ae(text):
list = re.findall("[ae]\w+", text)
return list
|
10927 | Test/png/10927.png | # Write a function to extract maximum and minimum k elements in the given tuple.
def extract_min_max(test_tup, K):
res = []
test_tup = list(test_tup)
temp = sorted(test_tup)
for idx, val in enumerate(temp):
if idx < K or idx >= len(temp) - K:
res.append(val)
res = tuple(res)
return res
|
11041 | Test/png/11041.png | # Write a python function to sort a list according to the second element in sublist.
def Sort(sub_li):
sub_li.sort(key=lambda x: x[1])
return sub_li
|
10889 | Test/png/10889.png | # Write a function to find the longest common prefix in the given set of strings.
def common_prefix_util(str1, str2):
result = ""
n1 = len(str1)
n2 = len(str2)
i = 0
j = 0
while i <= n1 - 1 and j <= n2 - 1:
if str1[i] != str2[j]:
break
result += str1[i]
i += 1
j += 1
return result
def common_prefix(arr, n):
prefix = arr[0]
for i in range(1, n):
prefix = common_prefix_util(prefix, arr[i])
return prefix
|
11180 | Test/png/11180.png | # Write a python function to check whether the given list contains consecutive numbers or not.
def check_Consecutive(l):
return sorted(l) == list(range(min(l), max(l) + 1))
|
10652 | Test/png/10652.png | def utime(self, *args, **kwargs):
os.utime(self.extended_path, *args, **kwargs)
|
10649 | Test/png/10649.png | def render(self, *args, **kwargs):
render_to = StringIO()
self.output(render_to, *args, **kwargs)
return render_to.getvalue()
|
10788 | Test/png/10788.png | # Write a function to find the nth tetrahedral number.
def tetrahedral_number(n):
return (n * (n + 1) * (n + 2)) / 6
|
10478 | Test/png/10478.png | def street_number():
length = int(random.choice(string.digits[1:6]))
return ''.join(random.sample(string.digits, length))
|
10664 | Test/png/10664.png | def install(self, package):
logger.debug('Installing ' + package)
shell.run(self.pip_path, 'install', package)
|
10921 | Test/png/10921.png | # Write a function to perform the concatenation of two string tuples.
def concatenate_strings(test_tup1, test_tup2):
res = tuple(ele1 + ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return res
|
10374 | Test/png/10374.png | def add_point_light(self, position, radius):
self.point_lights.append(PointLight(position, radius))
|
10812 | Test/png/10812.png | # Write a function to sort each sublist of strings in a given list of lists using lambda function.
def sort_sublists(input_list):
result = [sorted(x, key=lambda x: x[0]) for x in input_list]
return result
|