<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://courses.cs.taltech.ee/w/index.php?action=history&amp;feed=atom&amp;title=ITI0011%3AuniqueLines</id>
	<title>ITI0011:uniqueLines - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://courses.cs.taltech.ee/w/index.php?action=history&amp;feed=atom&amp;title=ITI0011%3AuniqueLines"/>
	<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=ITI0011:uniqueLines&amp;action=history"/>
	<updated>2026-04-06T20:48:41Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>http://courses.cs.taltech.ee/w/index.php?title=ITI0011:uniqueLines&amp;diff=597&amp;oldid=prev</id>
		<title>Ago: Uus lehekülg: &#039;Siin on koodinäide, kuidas võib lahendada teisipäeval, 16. septembril kell 08:00 toimunud praksis tehtud tunniülesannet:  &lt;source lang=&quot;java&quot;&gt; /**  * Whether the given one-di...&#039;</title>
		<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=ITI0011:uniqueLines&amp;diff=597&amp;oldid=prev"/>
		<updated>2014-09-22T03:23:04Z</updated>

		<summary type="html">&lt;p&gt;Uus lehekülg: &amp;#039;Siin on koodinäide, kuidas võib lahendada teisipäeval, 16. septembril kell 08:00 toimunud praksis tehtud tunniülesannet:  &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt; /**  * Whether the given one-di...&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Uus lehekülg&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Siin on koodinäide, kuidas võib lahendada teisipäeval, 16. septembril kell 08:00 toimunud praksis tehtud tunniülesannet:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Whether the given one-dimensional array which represents 3x3 table&lt;br /&gt;
 * has unique numbers in all its rows and columns.&lt;br /&gt;
 * @author Ago&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
public class T8 {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		System.out.println(uniqueLines(new int[]{1, 2, 3, 3, 1, 2, 2, 3, 1}));&lt;br /&gt;
		System.out.println(uniqueLines(new int[]{1, 2, 3, 3, 1, 2, 7, 2, 1}));&lt;br /&gt;
		System.out.println(uniqueLinesGeneral(new int[]{1, 2, 3, 3, 1, 2, 2, 3, 1}));&lt;br /&gt;
		System.out.println(uniqueLinesGeneral(new int[]{1, 2, 3, 3, 1, 2, 7, 2, 1}));&lt;br /&gt;
		System.out.println(uniqueLinesGeneral(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}));&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	/**&lt;br /&gt;
	 * Given an array of numbers, which represents 3x3 table&lt;br /&gt;
	 * whether all the rows and columns consist of unique numbers.&lt;br /&gt;
	 * @param board One-dimensional array which represents 3x3 table&lt;br /&gt;
	 * @return &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;, if all the columns and rows consist of unique numbers&lt;br /&gt;
	 * (not a single row or column consist of two or more of the same numbers)&lt;br /&gt;
	 * &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;, if at least one row or column has at least two elements with&lt;br /&gt;
	 * the same value.&lt;br /&gt;
	 */&lt;br /&gt;
	public static boolean uniqueLines(int[] board) {&lt;br /&gt;
		/*&lt;br /&gt;
		 * Table with indices:&lt;br /&gt;
		 * 0 1 2&lt;br /&gt;
		 * 3 4 5&lt;br /&gt;
		 * 6 7 8&lt;br /&gt;
		 */&lt;br /&gt;
		&lt;br /&gt;
		// let&amp;#039;s just check all 3 rows and columns within one for loop&lt;br /&gt;
		for (int i = 0; i &amp;lt; 3; i++) {&lt;br /&gt;
			// i * 3 will give us the first element in every row:&lt;br /&gt;
			// i = 0: 0 * 3 = 0&lt;br /&gt;
			// i = 1: 1 * 3 = 3&lt;br /&gt;
			// i = 2: 2 * 3 = 6&lt;br /&gt;
			&lt;br /&gt;
			// i * 3 + 1 =&amp;gt; second element in the row&lt;br /&gt;
			// i * 3 + 2 =&amp;gt; third element in the row&lt;br /&gt;
			&lt;br /&gt;
			// if first element in a row == second el in a row&lt;br /&gt;
			if (board[i * 3] == board[i * 3 + 1] ||&lt;br /&gt;
					// OR second element == third element&lt;br /&gt;
					board[i * 3 + 1] == board[i * 3 + 2] ||&lt;br /&gt;
					// OR first element == third element&lt;br /&gt;
					board[i * 3] == board[i * 3 + 2]) {&lt;br /&gt;
				// then we have a duplicate, let&amp;#039;s return false&lt;br /&gt;
				// as there is no point to check further&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
			&lt;br /&gt;
			// i gives us the first element in every column:&lt;br /&gt;
			// 0: first column&lt;br /&gt;
			// 1: second column&lt;br /&gt;
			// 2: third column&lt;br /&gt;
			&lt;br /&gt;
			// i + 3 =&amp;gt; second element in the column&lt;br /&gt;
			// i + 6 =&amp;gt; third element in the column&lt;br /&gt;
			&lt;br /&gt;
			// if the first element in column == second el in a col&lt;br /&gt;
			if (board[i] == board[i + 3] ||&lt;br /&gt;
					// OR second element == third element&lt;br /&gt;
					board[i + 3] == board[i + 6] ||&lt;br /&gt;
					// OR first element == third element&lt;br /&gt;
					board[i] == board[i + 6]) {&lt;br /&gt;
				// we have a duplicate, return false here&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		// if we reach here, there is no duplicate&lt;br /&gt;
		// so we can return true&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	/**&lt;br /&gt;
	 * General version of uniqueLines which accepts squares with&lt;br /&gt;
	 * different size than 3.&lt;br /&gt;
	 * @param board One-dimensional array which represents N x N&lt;br /&gt;
	 * table.&lt;br /&gt;
	 * @return &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if every row and column consists of&lt;br /&gt;
	 * only unique numbers, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; otherwise.&lt;br /&gt;
	 * If the input array does not present a square, &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
	 * is returned.&lt;br /&gt;
	 * @see T8#uniqueLines(int[])&lt;br /&gt;
	 */&lt;br /&gt;
	public static boolean uniqueLinesGeneral(int[] board) {&lt;br /&gt;
		int size = (int) Math.sqrt(board.length);&lt;br /&gt;
		// let&amp;#039;s check whether we have a full square&lt;br /&gt;
		if (size * size != board.length) {&lt;br /&gt;
			System.out.println(&amp;quot;Not a square!&amp;quot;);&lt;br /&gt;
			return false;&lt;br /&gt;
		}&lt;br /&gt;
		// below, rows and columns are checked within the same loop&lt;br /&gt;
		// it is basically the same as having separate for loops for rows&lt;br /&gt;
		// and separate loops for columns&lt;br /&gt;
		for (int axis1 = 0; axis1 &amp;lt; size; axis1++) {&lt;br /&gt;
			for (int axis2 = 0; axis2 &amp;lt; size; axis2++) {&lt;br /&gt;
				for (int comp = 0; comp &amp;lt; axis2; comp++) {&lt;br /&gt;
					if (board[axis1 * size + axis2] == board[axis1 * size + comp]) {&lt;br /&gt;
						// axis1 = row&lt;br /&gt;
						// axis2 = column&lt;br /&gt;
						// comp = compare current column value to previous columns&amp;#039; values&lt;br /&gt;
						return false;&lt;br /&gt;
					}&lt;br /&gt;
					if (board[axis2 * size + axis1] == board[comp * size + axis1]) {&lt;br /&gt;
						// axis1 = column&lt;br /&gt;
						// axis2 = row&lt;br /&gt;
						// comp = compare current row value to previous rows&amp;#039; values&lt;br /&gt;
						return false;&lt;br /&gt;
					}&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		// if some row/col had duplicates, we won&amp;#039;t make this far &lt;br /&gt;
		// (return ends the execution of this method)&lt;br /&gt;
		// so, if we reach here, there are no duplicates&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ago</name></author>
	</entry>
</feed>