|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START gae_python37_app] |
| 16 | +from flask import Flask, request |
| 17 | + |
| 18 | + |
| 19 | +# Enable cloud debugger |
| 20 | +# Adjust logging level to INFO |
| 21 | +try: |
| 22 | + import googleclouddebugger |
| 23 | + googleclouddebugger.enable() |
| 24 | + import logging |
| 25 | + logging.basicConfig(level=logging.INFO) |
| 26 | +except ImportError: |
| 27 | + pass |
| 28 | + |
| 29 | +# If `entrypoint` is not defined in app.yaml, App Engine will look for an app |
| 30 | +# called `app` in `main.py`. |
| 31 | +app = Flask(__name__) |
| 32 | + |
| 33 | + |
| 34 | +# There is a bug in the code. |
| 35 | +class StringProcessor(): |
| 36 | + def __init__(self, string): |
| 37 | + self._string = string |
| 38 | + |
| 39 | + def Reverse(self): |
| 40 | + if self._string is '': |
| 41 | + return '' |
| 42 | + |
| 43 | + chars = [c for c in self._string] |
| 44 | + left = 0 |
| 45 | + right = len(chars) - 1 |
| 46 | + while True: |
| 47 | + tmp = chars[left] |
| 48 | + chars[left] = chars[right] |
| 49 | + chars[right] = tmp |
| 50 | + if left >= right: |
| 51 | + break |
| 52 | + left += 1 |
| 53 | + right -= 1 |
| 54 | + |
| 55 | + return ''.join(chars) |
| 56 | + |
| 57 | + |
| 58 | +@app.route('/reverse_string', methods=['GET']) |
| 59 | +def ReverseString(): |
| 60 | + try: |
| 61 | + s = str(request.args.get('string')) |
| 62 | + except Exception as e: |
| 63 | + print(e) |
| 64 | + return 'Not a valid string!' |
| 65 | + |
| 66 | + current = StringProcessor(s).Reverse() |
| 67 | + expected = s[::-1] |
| 68 | + return ''' |
| 69 | + <table> |
| 70 | + <tr><th>Program Output:</th><th>{}</th></tr> |
| 71 | + <tr><th>Correct Output:</th><th>{}</th><tr> |
| 72 | + </table> |
| 73 | + '''.format(current, expected) |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +@app.route('/') |
| 78 | +def Hello(): |
| 79 | + """Return a friendly HTTP greeting.""" |
| 80 | + return ''' |
| 81 | + Hello! Enter a string to reverse it. |
| 82 | + <form method="get" action="reverse_string"> |
| 83 | + <p><input type=text name=string value="abcd"> |
| 84 | + <p><input type=submit> |
| 85 | + </form> |
| 86 | + ''' |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + # This is used when running locally only. When deploying to Google App |
| 91 | + # Engine, a webserver process such as Gunicorn will serve the app. This |
| 92 | + # can be configured by adding an `entrypoint` to app.yaml. |
| 93 | + app.run(host='127.0.0.1', port=8080, debug=True) |
| 94 | +# [END gae_python37_app] |
0 commit comments