Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Today our lesson is about rectangles and triangles.

you will be given as an input an n x n grid that is based on two characters # and * you have to classify it into : triangle or rectangle. where # represents a point and * represents a blank space.

Example input:

 #***
 ##**
 ###*
 ####

Output:

 triangle

Another example

  ##**
  ##**
  ##**
  ##**

Output:

  rectangle

Rules:

  • You should assume that the input is always either a triangle or a rectangle.
  • The only two characters you will receive are: * and #.
  • Assume that n < 100.
  • Be creative, this is a so the most up-voted answer will win.
share|improve this question
    
is the aspect ratio considered 1? What I mean is, square is n lines by n columns, right? –  mniip Feb 8 at 16:34
    
You say: one of the three cases. What's the third case? I only see a triangle or a rectangle. –  ProgramFOX Feb 8 at 16:35
    
@mniip no there could be 2x2 square in an 4x4 grid –  Mhmd Feb 8 at 16:36
    
@ProgramFOX a square, I didn't add an example for it, but it is still a case. –  Mhmd Feb 8 at 16:36
1  
All squares are rectangles so it would be correct to say rectangle and triangle only. –  user80551 Feb 8 at 16:55

5 Answers 5

up vote 5 down vote accepted

TSQL

Image recognition? Sounds like a job made for SQL :)

WITH cte AS (
  SELECT LEN(data)-LEN(REPLACE(data,'#','')) c FROM test_t
)
SELECT 
  CASE WHEN COUNT(DISTINCT c)>2                           THEN 'triangle' 
       WHEN SUM(CASE WHEN c=0 THEN 0 ELSE 1 END) = MAX(c) THEN 'square'
                                                          ELSE 'rectangle'
       END result
FROM cte;

An SQLfiddle with sample data and test.

share|improve this answer

Python

Creativity

Triangle

Forward     Reverse
#***        ####      ####
##**  +     *###  =   ####
###*        **##      ####
####        ***#      ####

Rectangle

Forward  Reverse
##**     ##**     ##**
##**  +  ##**  =  ##**
##**     ##**     ##**
##**     ##**     ##**

Implementation

s=raw_input();print["tri","rect"][all(a!=b for a,b in zip(s,s[::-1])if a!='\n')]+"angle" 
share|improve this answer

Python + Sympy

A problem in Geometry should be only solved mathematically

Creativity

if s if the number of "#" in the input stream

Triangle: n**2+n-2s=0 -> n should be an integer

Implementation

from sympy import *
s,n=2*raw_input().count("#"),Symbol('n');print["rect","tri"][float(solve(n**2+n-s)[0]).is_integer()]+"angle" 
share|improve this answer

C

Assumption: the input does not represent a single-line degenerate rectangle, e.g.

****
*##*
****
****

as arguably this is also a degenerate triangle, and violates "the input is always one of the two cases."

Logic: Count the number of points in the first two non-empty lines. If they are the same, it is a rectangle. Otherwise, it is a triangle.

Code:

#include <stdio.h>
#include <string.h>
int main() {
    char s[99];
    int cnt1=0, cnt2=0;
    int i;
    while(!cnt1) {
        gets(s);
        for(i=0;i<strlen(s);i++) cnt1+=(s[i]=='#');
    };
    gets(s);
    for(i=0;i<strlen(s);i++) cnt2+=(s[i]=='#');
    puts(cnt1==cnt2?"rectangle":"triangle");
    return 0;
}
share|improve this answer

Java

A simple straightforward java implementation. It scans the input looking for some 2x2 square which have # in two opposing corners with something not a # (presumable a *) is some other corner. I.E, it searchs for something that shows that the drawing has a diagonal-lined border.

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class TrianglesAndRectangles {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int r;
        while ((r = System.in.read()) != -1) {
            baos.write(r);
        }
        String[] lines = baos.toString().split("\n");
        System.out.println(hasTriangle(lines) ? "triangle" : "rectangle");
    }

    public static boolean hasTriangle(String[] lines) {
        for (int i = 0; i < lines.length - 1; i++) {
            for (int j = 0; j < lines[i].length() - 1; j++) {
                boolean a = lines[i].charAt(j) == '#';
                boolean b = lines[i].charAt(j + 1) == '#';
                boolean c = lines[i + 1].charAt(j) == '#';
                boolean d = lines[i + 1].charAt(j + 1) == '#';
                if ((a && d && (!c || !b)) || (b && c && (!a || !d))) return true;
            }
        }
        return false;
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.