Compare commits

...

2 Commits

Author SHA1 Message Date
NIKITA PANDEY
238fe8c494
Update receive_file.py (#8541)
* Update receive_file.py

Here are the changes I made:

Added the main() function and called it from if __name__ == "__main__" block. This makes it easier to test the code and import it into other programs.
Added socket.AF_INET as the first argument to socket.socket(). This specifies the address family to be used, which is necessary when using connect().
Changed print(f"{data = }") to print("Received:", len(data), "bytes"). This makes it clearer what's happening and how much data is being received.
Changed the final print statement to "Successfully received the file". This makes it more accurate and descriptive.
Moved the import statement to the top of the file. This is a common convention in Python.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-03-31 16:08:13 +02:00
Rohan Anand
a00492911a
added a problem on kadane's algo and its solution. (#8569)
* added kadane's algorithm directory with one problem's solution.

* added type hints

* Rename kaadne_algorithm/max_product_subarray.py to dynamic_programming/max_product_subarray.py

* Update dynamic_programming/max_product_subarray.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update max_product_subarray.py

* Update max_product_subarray.py

* Update dynamic_programming/max_product_subarray.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update max_product_subarray.py

* Update max_product_subarray.py

* Update max_product_subarray.py

* Update max_product_subarray.py

* Update max_product_subarray.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update max_product_subarray.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update max_product_subarray.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update max_product_subarray.py

* Update max_product_subarray.py

* Update dynamic_programming/max_product_subarray.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update dynamic_programming/max_product_subarray.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update max_product_subarray.py

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-03-31 13:17:13 +02:00
2 changed files with 64 additions and 7 deletions

View File

@ -0,0 +1,53 @@
def max_product_subarray(numbers: list[int]) -> int:
"""
Returns the maximum product that can be obtained by multiplying a
contiguous subarray of the given integer list `nums`.
Example:
>>> max_product_subarray([2, 3, -2, 4])
6
>>> max_product_subarray((-2, 0, -1))
0
>>> max_product_subarray([2, 3, -2, 4, -1])
48
>>> max_product_subarray([-1])
-1
>>> max_product_subarray([0])
0
>>> max_product_subarray([])
0
>>> max_product_subarray("")
0
>>> max_product_subarray(None)
0
>>> max_product_subarray([2, 3, -2, 4.5, -1])
Traceback (most recent call last):
...
ValueError: numbers must be an iterable of integers
>>> max_product_subarray("ABC")
Traceback (most recent call last):
...
ValueError: numbers must be an iterable of integers
"""
if not numbers:
return 0
if not isinstance(numbers, (list, tuple)) or not all(
isinstance(number, int) for number in numbers
):
raise ValueError("numbers must be an iterable of integers")
max_till_now = min_till_now = max_prod = numbers[0]
for i in range(1, len(numbers)):
# update the maximum and minimum subarray products
number = numbers[i]
if number < 0:
max_till_now, min_till_now = min_till_now, max_till_now
max_till_now = max(number, max_till_now * number)
min_till_now = min(number, min_till_now * number)
# update the maximum product found till now
max_prod = max(max_prod, max_till_now)
return max_prod

View File

@ -1,8 +1,9 @@
if __name__ == "__main__": import socket
import socket # Import socket module
sock = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 12312 port = 12312
sock.connect((host, port)) sock.connect((host, port))
@ -13,11 +14,14 @@ if __name__ == "__main__":
print("Receiving data...") print("Receiving data...")
while True: while True:
data = sock.recv(1024) data = sock.recv(1024)
print(f"{data = }")
if not data: if not data:
break break
out_file.write(data) # Write data to a file out_file.write(data)
print("Successfully got the file") print("Successfully received the file")
sock.close() sock.close()
print("Connection closed") print("Connection closed")
if __name__ == "__main__":
main()