Created
February 15, 2023 17:15
-
-
Save ifndefJOSH/2abe5d15a13eddc9eafeb7ec12447f5a to your computer and use it in GitHub Desktop.
PyQt5 Demo with some solutions to a couple easy job interview questions (palendrome and letter count)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from PyQt5 import QtWidgets | |
from PyQt5.QtWidgets import QMessageBox | |
def isPalendrome(aString): | |
for i in range(len(aString) // 2): | |
if aString[i] != aString[len(aString) - 1 - i]: | |
return False | |
return True | |
CHAR_SIZE = 1 # In bytes | |
def countLetters(word): | |
letterCount = [0 for _ in range(2 ** (8 * CHAR_SIZE))] | |
for c in word: | |
if ord(c) > 2 ** (8 * CHAR_SIZE): | |
print(f"Warning! Character {c} is outside supported character set!") | |
letterCount[ord(c)] += 1 | |
retString = "" | |
for i in range(len(letterCount)): | |
if letterCount[i] > 0: | |
retString = f"{retString}\nFor letter {chr(i)}, count is {letterCount[i]}" | |
return retString | |
# UI Stuff | |
def palendromeUIHandler(textBox): | |
msg = QMessageBox() | |
if isPalendrome(textBox.text()): | |
msg.setText(f"Word is {textBox.text()} a palendrome!") | |
msg.setIcon(QMessageBox.Information) | |
else: | |
msg.setText(f"Word {textBox.text()} is NOT a palendrome!") | |
msg.setIcon(QMessageBox.Warning) | |
msg.exec_() | |
def letterCountUIHandler(textBox): | |
msg = QMessageBox() | |
msg.setIcon(QMessageBox.Information) | |
msg.setText(countLetters(textBox.text())) | |
msg.exec_() | |
if __name__=='__main__': | |
app = QtWidgets.QApplication(sys.argv) | |
window = QtWidgets.QWidget() | |
window.resize(400, 400) | |
window.setWindowTitle("Never Gonna Give You Up") | |
layout = QtWidgets.QVBoxLayout() | |
layout.addWidget(QtWidgets.QLabel("Palendrome Word:")) | |
palendrome = QtWidgets.QLineEdit() | |
palendromeButton = QtWidgets.QPushButton("Check Palendrome!") | |
palendromeButton.clicked.connect(lambda : palendromeUIHandler(palendrome)) | |
layout.addWidget(palendrome) | |
layout.addWidget(palendromeButton) | |
layout.addWidget(QtWidgets.QLabel("Count the letters in this string:")) | |
letterCount = QtWidgets.QLineEdit() | |
letterCountButton = QtWidgets.QPushButton("Count the letters in this string!") | |
letterCountButton.clicked.connect(lambda : letterCountUIHandler(letterCount)) | |
layout.addWidget(letterCount) | |
layout.addWidget(letterCountButton) | |
window.setLayout(layout) | |
window.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment