Kaynağa Gözat

Add actual logic for python example

Joe Ceresini 7 yıl önce
ebeveyn
işleme
973c478088
1 değiştirilmiş dosya ile 44 ekleme ve 4 silme
  1. 44 4
      python/api.py

+ 44 - 4
python/api.py

@@ -3,8 +3,48 @@ import json
 
 app = Flask(__name__)
 
[email protected]("/contains_all_chars", methods=['POST'])
-def contains_all_chars():
-	# Just testing, essentially an echo server at this point
-	return json.dumps(request.get_json())
[email protected]("/contains_all_chars_a", methods=['POST'])
+def api_contains_all_chars_a():
+	response={'error': None}
 
+	try:
+		data = request.data
+		response['result'] = contains_all_chars(data)
+	except Exception,e:
+		response['error'] = "Error: %s" % e.message
+		response['result'] = None
+
+	return json.dumps(response)
+
+
[email protected]("/contains_all_chars_b", methods=['POST'])
+def api_contains_all_chars_b():
+	response={'error': None}
+
+	try:
+		data = request.data
+		response['result'] = contains_all_chars_iter(data)
+	except Exception,e:
+		response['error'] = "Error: %s" % e.message
+		response['result'] = None
+
+	return json.dumps(response)
+
+
+# Python sets have an issubset function
+def contains_all_chars(input):
+	wanted_chars=set("abcdefghijklmnopqrstuvwxyz")
+
+	return wanted_chars.issubset(input)
+
+# The above is actually faster, but doesn't show any skills whatsoever
+# This doesn't either...
+def contains_all_chars_iter(input):
+	wanted_chars=set("abcdefghijklmnopqrstuvwxyz")
+
+	for c in input:
+		if c in wanted_chars:
+			wanted_chars.remove(c)
+			if len(wanted_chars) == 0:
+				return True
+	return False