Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions function_type_and_modify_check_suffix.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from copy import deepcopy

{% raw %}
def order_repr(d):
'''Print in lexicographical order of repr if dict and set'''
if isinstance(d,dict):
d = sorted(d.items(), key=lambda x:order_repr(x[0]) )
return f"{{{', '.join(f'{order_repr(k)}: {order_repr(v)}' for k,v in d)}}}"
elif isinstance(d,set):
return f"{{{', '.join(map(order_repr,sorted(d, key=order_repr)))}}}"
else:
return repr(d)
{% endraw %}

def order_print(d):
print(order_repr(d))


import traceback
def except_wrap(func):
def inner_func(*args,**kwargs):
try:
func(*args,**kwargs)
except AssertionError as e:
print(e)
except Exception as e:
traceback.print_exception(e,limit=-1, file=sys.stdout)
return inner_func


def assert_equal(actual_out, expected_out, show=True):
assert expected_out is None or actual_out is not None,\
"No output returned.\nAre you returning the output? It seems like you are not returning anything."
assert actual_out == expected_out and type(actual_out) == type(expected_out),\
(
'Your output does not match expected output.\n'
f'Expected ouput (type: {type(expected_out).__name__}):\n{order_repr(expected_out)}\n'
f'Your output (type: {type(actual_out).__name__}):\n{order_repr(actual_out)}'
)
if show:
order_print(actual_out)

is_equal = except_wrap(assert_equal)

@except_wrap
def modify_check(func, in_obj, expected, should_modify=True):
in_obj_old = deepcopy(in_obj)
actual_out = func(in_obj)
if should_modify:
assert in_obj_old == expected or in_obj != in_obj_old,\
(
f'Input {type(in_obj).__name__} is not modified. You should modify the input {type(in_obj).__name__}.\n'
f'Original ({type(in_obj).__name__}):\n{order_repr(in_obj)}\n'
f'Expected modification:\n{order_repr(expected)}'
)
assert in_obj == expected,\
(
f'Incorrect modifcation of the input {type(in_obj).__name__}.\n'
f'Expected modification:\n{order_repr(expected)}\n'
f'Your modification:\n{order_repr(in_obj)}'
)
order_print(in_obj)
else:
assert_equal(actual_out, expected,show=False)
assert in_obj_old == in_obj,\
(
f'Input {type(in_obj).__name__} is modified. You shouldn\'t modify the input {type(in_obj).__name__}.\n'
f'Original input ({type(in_obj).__name__}):\n{order_repr(in_obj_old)}\n'
f'Your modification:\n{order_repr(in_obj)}'
)
order_print(actual_out)

import sys
exec(sys.stdin.read())

127 changes: 127 additions & 0 deletions q1-int_with_decimal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: Integer with decimal
---

# Problem Statement

Given an integer `n`, return the digits of absolute value of `n` separated by `.`

**Example**
```py3
integer_sep_decimal(123) # 1.2.3
integer_sep_decimal(-105) # 1.0.5
integer_sep_decimal(1) # 1
```

# Solution

```py3 test.py -r 'python test.py'
<prefix>
# some prefix
</prefix>
<template>
def integer_sep_decimal(n: int) -> str:
'''
Given an integer, return the digits separated by '.'

Arguments:
n: int - an integer

Return: str - string separated by '.'
'''
<los>...</los>
<sol>return '.'.join(str(abs(n)))</sol>
test = <los>...</los><sol>'test'</sol> #tests
</template>
<suffix>
# some suffix
</suffix>
<suffix_invisible>
{% include '../function_type_and_modify_check_suffix.py.jinja' %}
</suffix_invisible>
```

# Public Test Cases

## Input 1

```
is_equal(
integer_sep_decimal(123),
'1.2.3'
)
```

## Output 1

```
'1.2.3'
```

## Input 2

```
is_equal(
integer_sep_decimal(-105),
'1.0.5'
)
```

## Output 2

```
'1.0.5'
```

## Input 3

```
is_equal(
integer_sep_decimal(1),
'1'
)
```

## Output 3

```
'1'
```

# Private Test Cases

## Input 1

```
n = 0
is_equal(
integer_sep_decimal(n),
'0'
)

n = -1
is_equal(
integer_sep_decimal(n),
'1'
)

n = 100
is_equal(
integer_sep_decimal(n),
'1.0.0'
)
n = 1729
is_equal(
integer_sep_decimal(n),
'1.7.2.9'
)
```

## Output 1

```
'0'
'1'
'1.0.0'
'1.7.2.9'
```
126 changes: 126 additions & 0 deletions q2-odd_even_difference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
---
title: odd even difference
---

# Problem Statement

Given a list of two or more integers `L`, return the absolute difference of average of elements at odd indices and even indices
**Example**
```py3
odd_even_difference([1,2,3,4]) # 2
odd_even_difference([-1,0,5]) # 4
odd_even_difference([0,0,0,0]) # 0
```

# Solution

```py3 test.py -r 'python test.py'
<prefix>
# some prefix
</prefix>
<template>
def odd_even_difference(L: list) -> int:
'''
Given an list of two or more integers, return the absolute difference of average of elements at odd indices and even indices

Arguments:
L: list - a list of integers

Return: int - difference of elements
'''
<los>...</los>
<sol>return abs((sum(L[::2])/len(L[::2])) - (sum(L[1::2])/len(L[1::2])))</sol>
test = <los>...</los><sol>'test'</sol> #tests
</template>
<suffix>
# some suffix
</suffix>
<suffix_invisible>
{% include '../function_type_and_modify_check_suffix.py.jinja' %}
</suffix_invisible>
```

# Public Test Cases

## Input 1

```
is_equal(
odd_even_difference([1,2,3,4]),
1
)
```

## Output 1

```
1
```

## Input 2

```
is_equal(
odd_even_difference([-1,0,5]),
2
)
```

## Output 2

```
2
```

## Input 3

```
is_equal(
odd_even_difference([0,0,0,0]),
0
)
```

## Output 3

```
0
```

# Private Test Cases

## Input 1

```
L = [1,7,2,9]
is_equal(
odd_even_difference(L),
6.5
)

L = [-1,0,1,0]
is_equal(
odd_even_difference(L),
0
)

L = [-1,1]
is_equal(
odd_even_difference(L),
2
)

L = [1,1]
is_equal(
odd_even_difference(L),
0
)
```

## Output 1

```
6.5
0
2
0
Loading