From 606319b98ef6933105dc9b80000d6669e10160c6 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Tue, 4 Jun 2024 15:44:31 +0530 Subject: [PATCH] fix indentation for windows --- src/runtime/lpython_builtin.py | 51 ++++++++++----------- src/runtime/math.py | 26 +++++------ src/runtime/platform.py | 2 +- src/runtime/statistics.py | 82 +++++++++++++++++----------------- src/runtime/sys.py | 1 - 5 files changed, 77 insertions(+), 85 deletions(-) diff --git a/src/runtime/lpython_builtin.py b/src/runtime/lpython_builtin.py index 7678cb0de2..493b95a084 100644 --- a/src/runtime/lpython_builtin.py +++ b/src/runtime/lpython_builtin.py @@ -129,7 +129,7 @@ def pow(x: f64, y: i32) -> f64: def pow(x: bool, y: bool) -> i32: if y and not x: return 0 - + return 1 @overload @@ -146,7 +146,7 @@ def sum(arr: list[i32]) -> i32: """ sum: i32 sum = 0 - + i: i32 for i in range(len(arr)): sum += arr[i] @@ -159,7 +159,7 @@ def sum(arr: list[i64]) -> i64: """ sum: i64 sum = i64(0) - + i: i32 for i in range(len(arr)): sum += arr[i] @@ -172,7 +172,7 @@ def sum(arr: list[f32]) -> f32: """ sum: f32 sum = f32(0.0) - + i: i32 for i in range(len(arr)): sum += arr[i] @@ -185,7 +185,7 @@ def sum(arr: list[f64]) -> f64: """ sum: f64 sum = 0.0 - + i: i32 for i in range(len(arr)): sum += arr[i] @@ -618,7 +618,7 @@ def _lpython_str_capitalize(x: str) -> str: res += chr(ord(i) + 32) # Convert to lowercase using ASCII values else: res += i - + val: i32 val = ord(res[0]) if val >= ord('a') and val <= ord('z'): @@ -634,15 +634,15 @@ def _lpython_str_count(s: str, sub: str) -> i32: lps: list[i32] = [] s_len = len(s) sub_len = len(sub) - + if sub_len == 0: return s_len + 1 - + count = 0 - + for i in range(sub_len): lps.append(0) - + i = 1 _len = 0 while i < sub_len: @@ -656,7 +656,7 @@ def _lpython_str_count(s: str, sub: str) -> i32: else: lps[i] = 0 i += 1 - + j: i32 j = 0 i = 0 @@ -672,7 +672,7 @@ def _lpython_str_count(s: str, sub: str) -> i32: j = lps[j - 1] else: i = i + 1 - + return count @@ -766,15 +766,15 @@ def _lpython_str_title(s: str) -> str: else: result += ch capitalize_next = True - + return result def _lpython_str_istitle(s: str) -> bool: length: i32 = len(s) - + if length == 0: return False # Empty string is not in title case - + word_start: bool = True # Flag to track the start of a word ch: str only_whitespace: bool = True @@ -785,7 +785,6 @@ def _lpython_str_istitle(s: str) -> bool: word_start = False else: return False # Found an uppercase character in the middle of a word - elif ch.isalpha() and (ord('a') <= ord(ch) and ord(ch) <= ord('z')): only_whitespace = False if word_start: @@ -793,7 +792,6 @@ def _lpython_str_istitle(s: str) -> bool: word_start = False else: word_start = True - return True if not only_whitespace else False @overload @@ -807,10 +805,10 @@ def _lpython_str_find(s: str, sub: str) -> i32: res = -1 if s_len == 0 or sub_len == 0: return 0 if sub_len == 0 or (sub_len == s_len) else -1 - + for i in range(sub_len): lps.append(0) - + i = 1 _len = 0 while i < sub_len: @@ -824,7 +822,7 @@ def _lpython_str_find(s: str, sub: str) -> i32: else: lps[i] = 0 i += 1 - + j: i32 j = 0 i = 0 @@ -841,7 +839,6 @@ def _lpython_str_find(s: str, sub: str) -> i32: j = lps[j - 1] else: i = i + 1 - return res def _lpython_str_rstrip(x: str) -> str: @@ -886,7 +883,7 @@ def _lpython_str_split(x: str) -> list[str]: res.append(x_strip[start:start + ind]) start += ind + len(sep) return res - + @overload def _lpython_str_split(x: str, sep:str) -> list[str]: if len(sep) == 0: @@ -907,7 +904,7 @@ def _lpython_str_split(x: str, sep:str) -> list[str]: @overload def _lpython_str_replace(x: str, old:str, new:str) -> str: return _lpython_str_replace(x, old, new, len(x)) - + @overload def _lpython_str_replace(x: str, old:str, new:str, count: i32) -> str: @@ -925,7 +922,7 @@ def _lpython_str_replace(x: str, old:str, new:str, count: i32) -> str: lx: i32 = len(x) c: i32 = 0 t: i32 = -1 - + while(c bool: @overload def _lpython_str_endswith(s: str, suffix: str) -> bool: - if(len(suffix) > len(s)): return False - + i : i32 i = 0 while(i < len(suffix)): if(suffix[len(suffix) - i - 1] != s[len(s) - i - 1]): return False i += 1 - + return True @overload @@ -1143,4 +1139,3 @@ def list(s: str) -> list[str]: for i in range(len(s)): l.append(s[i]) return l - diff --git a/src/runtime/math.py b/src/runtime/math.py index 0a4f4a5c91..40e9c3fd86 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -10,7 +10,7 @@ def modf(x: f64) -> tuple[f64, f64]: """ Return fractional and integral parts of `x` as a pair. - + Both results carry the sign of x and are floats. """ return (x - f64(int(x)), float(int(x))) @@ -20,7 +20,7 @@ def factorial(x: i32) -> i32: """ Computes the factorial of `x`. """ - + result: i32 result = 0 if x < 0: @@ -104,7 +104,7 @@ def fsum(arr: list[i32]) -> f64: """ sum: f64 sum = 0.0 - + i: i32 for i in range(len(arr)): sum += float(arr[i]) @@ -117,7 +117,7 @@ def fsum(arr: list[i64]) -> f64: """ sum: f64 sum = 0.0 - + i: i32 for i in range(len(arr)): sum += float(arr[i]) @@ -130,7 +130,7 @@ def fsum(arr: list[f32]) -> f64: """ sum: f64 sum = 0.0 - + i: i32 for i in range(len(arr)): sum += float(arr[i]) @@ -143,7 +143,7 @@ def fsum(arr: list[f64]) -> f64: """ sum: f64 sum = 0.0 - + i: i32 for i in range(len(arr)): sum += arr[i] @@ -157,7 +157,7 @@ def prod(arr: list[i32]) -> f64: """ Return the product of the elements of `arr`. """ - + result: f64 result = 1.0 i: i32 @@ -170,7 +170,7 @@ def prod(arr: list[i64]) -> f64: """ Return the product of the elements of `arr`. """ - + result: f64 result = 1.0 i: i32 @@ -183,7 +183,7 @@ def prod(arr: list[f32]) -> f64: """ Return the product of the elements of `arr`. """ - + result: f64 result = 1.0 i: i32 @@ -196,7 +196,7 @@ def prod(arr: list[f64]) -> f64: """ Return the product of the elements of `arr`. """ - + result: f64 result = 1.0 i: i32 @@ -213,7 +213,7 @@ def dist(x: list[f64], y: list[f64]) -> f64: raise ValueError("Length of lists should be same") res: f64 res = 0.0 - + i: i32 for i in range(len(x)): res += (x[i] - y[i]) * (x[i] - y[i]) @@ -225,7 +225,7 @@ def comb(n: i32, k: i32) -> i32: Computes the result of `nCk`, i.e, the number of ways to choose `k` items from `n` items without repetition and without order. """ - + if n < k or n < 0: return 0 return i32(factorial(n)//(factorial(k)*factorial(n-k))) @@ -236,7 +236,7 @@ def perm(n: i32, k: i32) -> i32: Computes the result of `nPk`, i.e, the number of ways to choose `k` items from `n` items without repetition and with order. """ - + if n < k or n < 0: return 0 return i32(factorial(n)//factorial(n-k)) diff --git a/src/runtime/platform.py b/src/runtime/platform.py index 4c11b4977a..f408c53219 100644 --- a/src/runtime/platform.py +++ b/src/runtime/platform.py @@ -2,4 +2,4 @@ def python_implementation() -> str: return "LPython" def python_version() -> str: - return __LPYTHON_VERSION__ \ No newline at end of file + return __LPYTHON_VERSION__ diff --git a/src/runtime/statistics.py b/src/runtime/statistics.py index 61e1b9a9b5..66aadffd9e 100644 --- a/src/runtime/statistics.py +++ b/src/runtime/statistics.py @@ -12,7 +12,7 @@ def mean(x: list[i32]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): sum += float(x[i]) return sum/f64(k) @@ -29,10 +29,10 @@ def mean(x: list[i64]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): sum += float(x[i]) - + return sum/f64(k) @@ -47,7 +47,7 @@ def mean(x: list[f32]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): sum += float(x[i]) return sum/f64(k) @@ -64,7 +64,7 @@ def mean(x: list[f64]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): sum += x[i] return sum/f64(k) @@ -112,12 +112,12 @@ def geometric_mean(x: list[i32]) -> f64: product: f64 product = 1.0 i: i32 - + for i in range(k): if x[i] <= 0: raise Exception("geometric mean requires a non-empty dataset containing positive numbers") product *= float(x[i]) - + return product**(1/k) @overload @@ -131,7 +131,7 @@ def geometric_mean(x: list[i64]) -> f64: product: f64 product = 1.0 i: i32 - + for i in range(k): if x[i] <= i64(0): raise Exception("geometric mean requires a non-empty dataset containing positive numbers") @@ -150,7 +150,7 @@ def geometric_mean(x: list[f64]) -> f64: product: f64 product = 1.0 i: i32 - + for i in range(k): if x[i] <= 0.0: raise Exception("geometric mean requires a non-empty dataset containing positive numbers") @@ -169,14 +169,14 @@ def harmonic_mean(x: list[i32]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): if x[i] == 0: return 0.0 if x[i] < 0: raise Exception("Harmonic mean does not support negative values") sum += 1 / x[i] - + return f64(k)/sum @overload @@ -190,7 +190,7 @@ def harmonic_mean(x: list[i64]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): if x[i] == i64(0): return 0.0 @@ -210,14 +210,14 @@ def harmonic_mean(x: list[f64]) -> f64: sum: f64 sum = 0.0 i: i32 - + for i in range(k): if x[i] == 0.0: return 0.0 if x[i] < 0.0: raise Exception("Harmonic mean does not support negative values") sum += 1.0 / x[i] - + return f64(k) / sum @@ -227,15 +227,15 @@ def mode(x: list[i32]) -> i32: k: i32 = len(x) c: i32 count: dict[i32, i32] = {0: 0} - + # insert keys in the dictionary for c in range(k): count[x[c]] = 0 - + # update the frequencies for c in range(k): count[x[c]] = count[x[c]] + 1 - + max_count: i32 = 0 ans: i32 for c in range(k): @@ -249,15 +249,15 @@ def mode(x: list[i64]) -> i64: k: i32 = len(x) c: i32 count: dict[i64, i32] = {i64(0): 0} - + # insert keys in the dictionary for c in range(k): count[x[c]] = 0 - + # update the frequencies for c in range(k): count[x[c]] = count[x[c]] + 1 - + max_count: i32 = 0 ans: i64 for c in range(k): @@ -383,17 +383,17 @@ def correlation(x: list[i32], y: list[i32]) -> f64: raise Exception("correlation requires at least two data points") xmean: f64 = mean(x) ymean: f64 = mean(y) - + sxy: f64 = 0.0 i: i32 for i in range(n): sxy += (f64(x[i]) - xmean) * (f64(y[i]) - ymean) - + sxx: f64 = 0.0 j: i32 for j in range(n): sxx += (f64(x[j]) - xmean) ** 2.0 - + syy: f64 = 0.0 k: i32 for k in range(n): @@ -415,17 +415,17 @@ def correlation(x: list[f64], y: list[f64]) -> f64: raise Exception("correlation requires at least two data points") xmean: f64 = mean(x) ymean: f64 = mean(y) - + sxy: f64 = 0.0 i: i32 for i in range(n): sxy += (x[i] - xmean) * (y[i] - ymean) - + sxx: f64 = 0.0 j: i32 for j in range(n): sxx += (f64(x[j]) - xmean) ** 2.0 - + syy: f64 = 0.0 k: i32 for k in range(n): @@ -472,7 +472,6 @@ def covariance(x: list[f64], y: list[f64]) -> f64: @overload def linear_regression(x: list[i32], y: list[i32]) -> tuple[f64, f64]: - """ Returns the slope and intercept of simple linear regression parameters estimated using ordinary least squares. @@ -484,33 +483,32 @@ def linear_regression(x: list[i32], y: list[i32]) -> tuple[f64, f64]: raise Exception('linear regression requires at least two data points') xmean: f64 = mean(x) ymean: f64 = mean(y) - + sxy: f64 = 0.0 i: i32 for i in range(n): sxy += (f64(x[i]) - xmean) * (f64(y[i]) - ymean) - + sxx: f64 = 0.0 j: i32 for j in range(n): sxx += (f64(x[j]) - xmean) ** 2.0 - + slope: f64 - + if sxx == 0.0: raise Exception('x is constant') else: slope = sxy / sxx - + intercept: f64 = ymean - slope * xmean - + LinReg: tuple[f64, f64] = (slope, intercept) - + return LinReg @overload def linear_regression(x: list[f64], y: list[f64]) -> tuple[f64, f64]: - """ Returns the slope and intercept of simple linear regression parameters estimated using ordinary least squares. @@ -522,26 +520,26 @@ def linear_regression(x: list[f64], y: list[f64]) -> tuple[f64, f64]: raise Exception('linear regression requires at least two data points') xmean: f64 = mean(x) ymean: f64 = mean(y) - + sxy: f64 = 0.0 i: i32 for i in range(n): sxy += (x[i] - xmean) * (y[i] - ymean) - + sxx: f64 = 0.0 j: i32 for j in range(n): sxx += (f64(x[j]) - xmean) ** 2.0 - + slope: f64 - + if sxx == 0.0: raise Exception('x is constant') else: slope = sxy / sxx - + intercept: f64 = ymean - slope * xmean - + LinReg: tuple[f64, f64] = (slope, intercept) - + return LinReg diff --git a/src/runtime/sys.py b/src/runtime/sys.py index 505926bb2a..3dd57bc0c8 100644 --- a/src/runtime/sys.py +++ b/src/runtime/sys.py @@ -4,7 +4,6 @@ def exit(error_code: i32): """ Exits the program with an error code `error_code`. """ - quit(error_code) # >----------------------------------- argv ----------------------------------->