Created
August 27, 2013 21:39
-
-
Save Experiment5X/6359517 to your computer and use it in GitHub Desktop.
Square challenge solutions from TheTechGame.com
This file contains hidden or 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
#include <stdio.h> | |
void PrintStarLine(int len) | |
{ | |
for (int i = 0; i < len; i++) | |
printf("*"); | |
printf("\n"); | |
} | |
void PrintBodyLine(int len) | |
{ | |
printf("*"); | |
// print out the center of the square, the .+ characters | |
for (int i = 0; i < (len - 2) / 2; i++) | |
printf(".+"); | |
printf(".*\n"); | |
} | |
int DrawSquare(int size) | |
{ | |
// make sure the number is positive and no less than 3 | |
if (size < 3 || (size & 1) == 0) | |
return -1; | |
// print the top | |
PrintStarLine(size); | |
// print the body of the square | |
for (int i = 0; i < size - 2; i++) | |
PrintBodyLine(size); | |
// print the bottom | |
PrintStarLine(size); | |
return 0; | |
} | |
int main() | |
{ | |
int size; | |
// get the size from the user | |
printf("Square Size: "); | |
scanf("%i", &size); | |
if (DrawSquare(size) != 0) | |
printf("Invalid size. It must be an odd number greater than 1.\n"); | |
return 0; | |
} |
This file contains hidden or 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
import sys | |
def GetStarLine(size): | |
return '*' * squareSize + '\n' | |
def GetBodyLine(squareSize): | |
line = '*' | |
line += '.+' * ((squareSize - 2) / 2) + '.' | |
line += '*\n' | |
return line | |
# get the size from the user | |
squareSize = int(raw_input('Square Size: ')) | |
square = '' | |
# make sure the square size is an odd number greater than 1 | |
if squareSize < 3 or (squareSize & 1) == 0: | |
sys.exit('Invalid size. It must be an odd number greater than 1.') | |
# top line | |
square += GetStarLine(squareSize) | |
# body | |
square += GetBodyLine(squareSize) * (squareSize - 2) | |
# bottom line | |
square += GetStarLine(squareSize) | |
print square |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment