2
\$\begingroup\$

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.
\$\endgroup\$
6
  • \$\begingroup\$ is the aspect ratio considered 1? What I mean is, square is n lines by n columns, right? \$\endgroup\$ Commented Feb 8, 2014 at 16:34
  • \$\begingroup\$ @mniip no there could be 2x2 square in an 4x4 grid \$\endgroup\$ Commented Feb 8, 2014 at 16:36
  • \$\begingroup\$ Could it for example also be an inverted square, ie a hole? \$\endgroup\$ Commented Feb 8, 2014 at 17:01
  • \$\begingroup\$ @JoachimIsaksson no the square(rectangle) should be filled completely. \$\endgroup\$ Commented Feb 8, 2014 at 17:03
  • \$\begingroup\$ Is it guaranteed that all triangles will have exactly one character at their vertex? \$\endgroup\$ Commented Feb 9, 2014 at 13:02

5 Answers 5

5
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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" 
\$\endgroup\$
2
\$\begingroup\$

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" 
\$\endgroup\$
1
\$\begingroup\$

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; } 
\$\endgroup\$
1
\$\begingroup\$

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; } } 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.