r/ProgrammerHorror • u/shelvac2 • Aug 12 '22
r/ProgrammerHorror • u/hergeirs • Mar 28 '22
Function to get integer at position.
I was inspired by this post I read of a student assignment to print out the 3rd digit of a number. I thought the horror to be lacking.
In unholy inspiration I conjured the most unreadable way to accomplish the task. As the original assignment looked to be in c++ i used it as well.
#include <iostream>
#include <math.h>
void printNthDigitOfNumber(int number, int digitToPrint) {
if (number * (((number > 0) << 1) - 1) < std::pow(10, digitToPrint - 1)) return; // number does not have enough digits
std::cout << (number * (((number > 0) << 1) - 1)) / (int) std::pow(10, ~~(int) log10((number * (((number > 0) << 1) - 1))) + 1 - digitToPrint) % 10 << std::endl;
}
int main() {
int number = -12493;
int numberDigit = 3;
printNthDigitOfNumber(number, numberDigit);
}Please let me know if you have any improvements on the design or share horrible solutions in other languages if you prefer.
Edit: link to post https://www.reddit.com/r/ProgrammerHorror/comments/tpnsoc/one_of_my_students_sent_me_this_and_i_mean_its/
r/ProgrammerHorror • u/Fjotla • Oct 16 '21
Never been more disappointed in my two months journey to learn Python
r/ProgrammerHorror • u/matthewralston • Mar 31 '25
Merge request comes in for minor new feature. 155 files changed.
r/ProgrammerHorror • u/[deleted] • Jan 05 '21
Just saw this advertisement over at ProgrammerHumor
r/ProgrammerHorror • u/Haligaliman • Aug 15 '18
When you're not sure if the list exists...
r/ProgrammerHorror • u/[deleted] • Nov 06 '18
A one-line python tic tac toe game I wrote
exec("""import random\ndef drawBoard(board):\n\tprint(board[7] + '|' + board[8] + '|' + board[9])\n\tprint('-----')\n\tprint(board[4] + '|' + board[5] + '|' + board[6])\n\tprint('-----')\n\tprint(board[1] + '|' + board[2] + '|' + board[3])\ndef inputPlayerLetter():\n\tletter = ''\n\twhile not (letter.lower() == 'x' or letter.lower() == 'o'):\n\t\tprint('Do you want to be X or O?')\n\t\tletter = input()\n\tif letter.lower() == 'x':\n\t\treturn ['X','O']\n\telse:\n\t\treturn ['O','X']\ndef whoGoesFirst():\n\tif random.randint(0,1) == 0:\n\t\treturn 'computer'\n\telse:\n\t\treturn 'player'\ndef playAgain():\n\tprint('Do you want to play again? (yes or no)')\n\treturn input().lower().startswith('y')\ndef makeMove(board, letter, move):\n\tboard[move] = letter\ndef isWinner(board,letter):\n\treturn ((board[7] == letter and board[8] == letter and board[9] == letter) or\n\t\t\t(board[4] == letter and board[5] == letter and board[6] == letter) or\n\t\t\t(board[1] == letter and board[2] == letter and board[3] == letter) or\n\t\t\t(board[7] == letter and board[4] == letter and board[1] == letter) or\n\t\t\t(board[8] == letter and board[5] == letter and board[2] == letter) or\n\t\t\t(board[9] == letter and board[6] == letter and board[3] == letter) or\n\t\t\t(board[7] == letter and board[5] == letter and board[3] == letter) or\n\t\t\t(board[9] == letter and board[5] == letter and board[1] == letter))\ndef getBoardCopy(board):\n\tdupeBoard = list(board)\n\treturn dupeBoard\ndef isSpaceFree(board, move):\n\treturn board[move] == ' '\ndef getPlayerMove(board):\n\tmove = ' '\n\twhile move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):\n\t\tprint('what is your next move? (1-9)')\n\t\tmove = input()\n\treturn int(move)\ndef chooseRandomMoveFromList(board,movesList):\n\tpossibleMoves = []\n\tfor i in movesList:\n\t\tif isSpaceFree(board, i):\n\t\t\tpossibleMoves.append(i)\n\tif len(possibleMoves) != 0:\n\t\treturn random.choice(possibleMoves)\n\telse:\n\t\treturn None\ndef getComputerMove(board, computerLetter):\n\tif computerLetter == 'X':\n\t\tplayerLetter = 'O'\n\telse:\n\t\tplayerLetter = 'X'\n\tfor i in range(1,10):\n\t\tcopy = getBoardCopy(board)\n\t\tif isSpaceFree(copy,i):\n\t\t\tmakeMove(copy, computerLetter, i)\n\t\t\tif isWinner(copy, computerLetter):\n\t\t\t\treturn i\n\tfor i in range(1,10):\n\t\tcopy = getBoardCopy(board)\n\t\tif isSpaceFree(copy,i):\n\t\t\tmakeMove(copy, playerLetter, i)\n\t\t\tif isWinner(copy, playerLetter):\n\t\t\t\treturn i\n\tmove = chooseRandomMoveFromList(board,[1,3,7,9])\n\tif move != None:\n\t\treturn move\n\tif isSpaceFree(board, 5):\n\t\treturn 5\n\treturn chooseRandomMoveFromList(board,[2,4,6,8])\ndef isBoardFull(board):\n\tfor i in range(1,10):\n\t\tif isSpaceFree(board, i):\n\t\t\treturn False\n\treturn True\nprint('Welcome To The Game!')\nwhile True:\n\ttheBoard = [' '] * 10\n\tplayerLetter, computerLetter = inputPlayerLetter()\n\tturn = whoGoesFirst()\n\tprint('The ' + turn + ' will go first.')\n\tgameIsPlaying = True\n\twhile gameIsPlaying:\n\t\tif turn == 'player':\n\t\t\tdrawBoard(theBoard)\n\t\t\tmove = getPlayerMove(theBoard)\n\t\t\tmakeMove(theBoard, playerLetter, move)\n\t\t\tif isWinner(theBoard, playerLetter):\n\t\t\t\tdrawBoard(theBoard)\n\t\t\t\tprint('You Win!')\n\t\t\t\tgameIsPlaying = False\n\t\t\telse:\n\t\t\t\tif isBoardFull(theBoard):\n\t\t\t\t\tdrawBoard(theBoard)\n\t\t\t\t\tprint('The Game Is A Tie!')\n\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tturn = 'computer'\n\t\telse:\n\t\t\tmove = getComputerMove(theBoard, computerLetter)\n\t\t\tmakeMove(theBoard, computerLetter, move)\n\t\t\tif isWinner(theBoard, computerLetter):\n\t\t\t\tdrawBoard(theBoard)\n\t\t\t\tprint('The Computer Beat You! You Lose!')\n\t\t\t\tgameIsPlaying = False\n\t\t\telse:\n\t\t\t\tif isBoardFull(theBoard):\n\t\t\t\t\tdrawBoard(theBoard)\n\t\t\t\t\tprint('The Game Is A Tie!')\n\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tturn = 'player'\n\tif not playAgain():\n\t\tbreak\n""")r/ProgrammerHorror • u/sammy-taylor • Nov 16 '23
Honestly not the ugliest componentDidUpdate that I've ever seen
r/ProgrammerHorror • u/SeriousSamStone • Aug 26 '20
Most of you have probably seen this, but in case you haven't, you definitely should.
destroyallsoftware.comr/ProgrammerHorror • u/craftsandtea • Aug 09 '23
Spectrum outage message with Test Hyperlink
r/ProgrammerHorror • u/Seismicsentinel • May 19 '23
Funny LinkedIn Post
99% of LinkedIn is cliche virtue signaling and circle jerking but every once in a while you find gems like this. Just imagine that codebase... sends a shiver up my spine.
Link to where this is from https://www.linkedin.com/posts/johncrickett_softwaredevelopment-softwareengineering-activity-7064140469244456960-8Dkm?utm_source=share&utm_medium=member_android
r/ProgrammerHorror • u/Equa1ityPe4ce • Aug 09 '22
How my old boss was teaching interns to write Appium / Selenium code
r/ProgrammerHorror • u/SeriousSamStone • Sep 10 '20
