Skip to content

Create bra_batuhan_yavuz.py #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions Week05/bra_batuhan_yavuz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from flask import Flask, request, jsonify

app = Flask(__name__)

class BinaryRepresentation:
def __init__(self, number):
if not isinstance(number, (int, float)):
raise TypeError("Invalid input type, expected int, float, or bool")
if isinstance(number, bool):
raise TypeError("Invalid input type, expected int or float")
self.number = number


def integer2binary(self):
if not isinstance(self.number, (int, float)):
raise TypeError("Invalid input type, expected int or float")
integer_part = int(self.number)
return bin(integer_part)[2:]

def decimal2binary(self):
if not isinstance(self.number, (int, float)):
raise TypeError("Invalid input type, expected int or float")
decimal_part = self.number - int(self.number)
binary_str = ""
for _ in range(10):
decimal_part *= 2
binary_str += str(int(decimal_part))
decimal_part -= int(decimal_part)
return binary_str

def __str__(self):
if not isinstance(self.number, (int, float)):
raise TypeError("Invalid input type, expected int or float")
return f"{self.integer2binary()}.{self.decimal2binary()}"

@app.route('/binary_representation', methods=['GET'])
def binary_representation():
try:
number = float(request.args.get('number'))
except (TypeError, ValueError):
return jsonify({'error': 'Invalid number parameter. Must be a float.'}), 400

binary_obj = BinaryRepresentation(number)
binary_str = str(binary_obj)

return jsonify({'binary_representation': binary_str})

if __name__ == '__main__':
app.run(debug=True)