8000 feat(few-shot): add a few-shot example 10 for #1 · bigcode-project/selfcodealign@62cd2ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 62cd2ae

Browse files
committed
feat(few-shot): add a few-shot example 10 for #1
1 parent e1b166b commit 62cd2ae

File tree

1 file changed

+139
-1
lines changed

1 file changed

+139
-1
lines changed

prompts/self-ossinstruct-fewshot.txt

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,4 +681,142 @@ assert create_folds(range(12), 2) == [range(0, 6), range(6, 12)]
681681
assert create_folds(range(8), 2) == [range(0, 4), range(4, 8)]
682682
assert create_folds(range(25), -5) == []
683683
assert create_folds(range(6), 2) == [range(0, 3), range(3, 6)]
684-
```
684+
```
685+
686+
### Example 10
687+
[Code]
688+
def duration(string):
689+
match = re.match(PATTERN, string)
690+
if not match:
691+
raise ValueError('Invalid duration: {}'.format(string))
692+
693+
suffix = match.group(2)
694+
if suffix not in SUFFIX_MAP:
695+
raise ValueError('Invalid duration suffix: {}'.format(string))
696+
697+
return int(match.group(1)) * SUFFIX_MAP[suffix]
698+
699+
[Property]
700+
category: code generation (function implementation)
701+
language: Python
702+
concepts: regular expression, unit conversion, error handling
703+
704+
[Instruction]
705+
Create a Python function that converts a string representation of a time duration into its total number of seconds.
706+
The input string should follow a format that includes a numeric part followed by a time unit suffix without any spaces between.
707+
The time unit can be seconds in the form of 's' or 'sec', minutes in the form of 'm' or 'min', hours in the form of 'h' or 'hour', and days in the form of 'd' or 'day' (units are in small case).
708+
If the input format is invalid, the function should raise a ValueError.
709+
710+
```python
711+
assert parse_duration('15s') == 15
712+
assert parse_duration('2min') == 120
713+
assert parse_duration('1h') == 3600
714+
assert parse_duration('1d') == 86400
715+
try:
716+
parse_duration('30')
717+
except ValueError as e:
718+
pass
719+
```
720+
721+
[Response]
722+
[Reasoning]
723+
The desired function aims to transform a string representation into its total number of seconds. The implementation can be split into two parts:
724+
725+
Input validation:
726+
1. The input string includes two parts: a number as the prefix and the unit as the suffix.
727+
2. We can use regular expression to assert the prefix must be a number (d+) and suffix must be one of s/sec/m/min/h/hour/d/day.
728+
3. If the input string cannot the regular expression, we raise a ValueError.
729+
730+
Result computation:
731+
1. Based on the matches of regular expression, we capture the number and unit part.
732+
1. We convert the unit to seconds: 's'/'sec' -> 1 second, 'm'/'min' -> 60 seconds, 'h'/'hour' -> 3600 seconds, 'd'/'day' -> 86400 seconds.
733+
2. We multiply the seconds of the unit and the prefix number and return it as an integer.
734+
735+
[Implementation]
736+
Here's how you can implement this:
737+
738+
```python
739+
import re
740+
741+
def parse_duration(duration_str):
742+
# Define a regex pattern to match the input string
743+
pattern = re.compile(r"(\d+)(s|sec|m|min|h|hour|d|day)$")
744+
match = pattern.match(duration_str)
745+
746+
# Raise an error if the input string does not match the pattern
747+
if not match:
748+
raise ValueError(f"Invalid duration: {duration_str}")
749+
750+
# Extract the numeric part and the suffix from the matched groups
751+
value, suffix = match.groups()
752+
value = int(value)
753+
754+
# Define a dictionary mapping suffixes to their respective number of seconds
755+
suffix_map = {
756+
's': 1, 'sec': 1,
757+
'm': 60, 'min': 60,
758+
'h': 3600, 'hour': 3600,
759+
'd': 86400, 'day': 86400
760+
}
761+
762+
# Calculate and return the total number of seconds
763+
return value * suffix_map[suffix]
764+
```
765+
766+
[Explanation]
767+
The implementation begins by defining a regular expression pattern that expects a numeric value followed by a valid time unit suffix.
768+
The function then tries to match this pattern against the input string.
769+
If the match is successful, it extracts the numeric value and the suffix.
770+
A dictionary (suffix_map) maps each suffix to the number of seconds it represents.
771+
Using this dictionary, the function calculates the total number of seconds corresponding to the input and returns it.
772+
773+
[Tests]
774+
You can test your code with the provided assertions to verify that the function works correctly:
775+
776+
```python
777+
# Test additional valid inputs with different suffix representations
778+
assert parse_duration('10sec') == 10, "Check for 'sec' suffix"
779+
assert parse_duration('3min') == 180, "Check for 'min' suffix"
780+
assert parse_duration('5h') == 18000, "Check for 'h' suffix"
781+
assert parse_duration('2hour') == 7200, "Check for 'hour' suffix"
782+
assert parse_duration('1day') == 86400, "Check for 'day' suffix"
783+
784+
# Test edge cases
785+
assert parse_duration('0s') == 0, "Zero duration"
786+
assert parse_duration('0min') == 0, "Zero minutes"
787+
788+
# Test inputs with leading zeros
789+
assert parse_duration('001s') == 1, "Leading zeros"
790+
assert parse_duration('010min') == 600, "Leading zeros with minutes"
791+
792+
# Test case sensitivity and invalid suffix handling
793+
try:
794+
parse_duration('10S')
795+
except ValueError:
796+
pass
797+
798+
try:
799+
parse_duration('2hours')
800+
except ValueError:
801+
pass
802+
803+
try:
804+
parse_duration('5 mins')
805+
except ValueError:
806+
pass
807+
808+
# Test incorrect formats
809+
try:
810+
parse_duration('h1')
811+
except ValueError:
812+
pass
813+
814+
try:
815+
parse_duration('100')
816+
except ValueError:
817+
pass
818+
819+
# Test extremely large numbers
820+
assert parse_duration('1000000s') == 1000000, "Very large number of seconds"
821+
```
822+

0 commit comments

Comments
 (0)
0