Commit b21a03d2 authored by Dr. Martin Goik's avatar Dr. Martin Goik

New pangram exercise

parent a0282707
......@@ -829,6 +829,72 @@ Mark|frequency
</qandadiv>
</qandaset>
<qandaset defaultlabel="qanda" xml:id="sd1QandaPangram">
<title><acronym>Pangram</acronym> checker</title>
<qandadiv>
<qandaentry>
<question>
<para>A <acronym>pangram</acronym> is a string containing all
letters of a given alphabet at least once. As an example we consider
the alphabet of <xref linkend="glo_ASCII"/> letters <code>{'a', 'b',
..., 'z'}</code> ignoring case. Then the following string is a
<acronym>pangram</acronym>:</para>
<blockquote>
<para>The quick brown fox jumps over the lazy dog</para>
</blockquote>
<tip>
<orderedlist>
<listitem>
<para>Like in <xref linkend="sd1QandaMarkFrequencies"/> create
an array of sufficient size regarding each character as an
index value.</para>
</listitem>
<listitem>
<para>According to the <xref linkend="glo_ASCII"/> table all
characters <code>{'a', 'b', ..., 'z'}</code> form a gapless
sequence of integer values {97, 98,..., 122}.</para>
</listitem>
<listitem>
<para><methodname
xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toCharArray--">toCharArray()</methodname>
allows for de-composing a String into a corresponding array of
char values.</para>
</listitem>
<listitem>
<para>Normalize strings with respect to case using <methodname
xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toLowerCase--">toLowerCase(...)</methodname>.</para>
</listitem>
</orderedlist>
</tip>
</question>
<answer>
<annotation role="make">
<para role="eclipse">P/Sd1/Array/pangram</para>
</annotation>
<para>In addition to the above <quote>regular</quote> solution we
also provide an approach featuring <xref linkend="glo_Java"/>
streams with respect to the upcoming <quote>Software development
2</quote> lecture:</para>
<programlisting language="java">static public boolean isPangram2(final String s) {
return 26 == s.toLowerCase().chars()
.filter(i -&gt; i &gt;= 'a' &amp;&amp; i &lt;= 'z')
.distinct()
.count();
}</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<figure xml:id="sd1_array_fig_arrayDefinition">
<title>Array</title>
......
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm_stuttgart.mi</groupId>
<artifactId>marking</artifactId>
<version>0.9</version>
<name>pangram</name>
<url>https://freedocs.mi.hdm-stuttgart.de</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<linksource>true</linksource>
<additionalOptions>
<additionalOption>-html5</additionalOption>
</additionalOptions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.mi.pangram.PangramChecker</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package de.hdm_stuttgart.mi.pangram;
/**
* <p>Checking whether a given ASCII String is a pangram.</p>
*/
class PangramChecker {
static private final byte alphabetCharCount = 'z' - 'a' + 1; // 26 characters {'a', 'b', ..., 'z'}
static private final char alphabetStartIndex = 'a';
/**
* <p>Is a given string a pangram with respect to the set of characters {'a', 'b', ..., 'z'} or not?</p>
*
* @param s String to be evaluated.
* @return true if string is a pangram, false otherwise.
*/
static public boolean isPangram(final String s) {
final boolean[] charactersFound = // Reserve one boolean per alphabet character, initialized to false.
new boolean[alphabetCharCount];
int distinctCharacterCount = 0; // Number of different characters found so far.
for (final char c : s.toLowerCase().toCharArray()) {
if ('a' <= c && c <= 'z' // Character belongs to alphabet?
&& !charactersFound[c - 'a']) { // Character already discovered during call?
distinctCharacterCount++; // Yet another distinct one.
charactersFound[c - 'a'] = true; // Mark alphabet character as being found.
}
}
return distinctCharacterCount == alphabetCharCount; // Did we find all 26?
}
/**
* Same as {@link #isPangram(String)} using a stream based implementation
* @param s See {@link #isPangram(String)}
* @return See {@link #isPangram(String)}
*/
static public boolean isPangram2(final String s) {
return alphabetCharCount == s.toLowerCase().chars()
.filter(i -> i >= 'a' && i <= 'z')
.distinct()
.count();
}
/**
* Main entry point
* @param args Unused.
*/
public static void main(String[] args) {
System.out.println(isPangram("The quick brown fox jumps over the lazy dog"));
System.out.println(isPangram("The quick brown fox jumps over the lazy"));
}
}
package de.hdm_stuttgart.mi.pangram;
import org.junit.Assert;
import org.junit.Test;
/**
* Hunting for pangrammatic strings.
*/
@SuppressWarnings({"UnusedDeclaration"})
public class PangramTest {
@Test
public void testNegative() {
Assert.assertFalse(PangramChecker.isPangram("Two driven jocks help fax my big"));
Assert.assertFalse(PangramChecker.isPangram("Pack box with five dozen liquor jugs"));
Assert.assertFalse(PangramChecker.isPangram(" five boxing wizards jump quickly"));
Assert.assertFalse(PangramChecker.isPangram("Bright vixens jump; dozy quack"));
Assert.assertFalse(PangramChecker.isPangram("Jackdaws love my big sphinx quartz"));
Assert.assertFalse(PangramChecker.isPangram("John quickly extemporized five bags"));
Assert.assertFalse(PangramChecker.isPangram("Waltz, nymph, for quick jigs Bud"));
Assert.assertFalse(PangramChecker.isPangram("Quick wafting zephyrs bold Jim"));
Assert.assertFalse(PangramChecker.isPangram("Brown jars prevented mixture from freezing uickly"));
Assert.assertFalse(PangramChecker.isPangram("Fred specialized the job of making very quaint toys"));
Assert.assertFalse(PangramChecker.isPangram("Sixty zippers were quickly picked from woven jute bag"));
Assert.assertFalse(PangramChecker.isPangram("We promptly judged antique ivory buckles for next prize"));
Assert.assertFalse(PangramChecker.isPangram("J.Q. Schwartz flung V.D. Pike box"));
Assert.assertFalse(PangramChecker.isPangram("Viewing quizzical abstracts mixed hefty jocks"));
Assert.assertFalse(PangramChecker.isPangram("Farmer jack realized that yellow quilts were expensive"));
Assert.assertFalse(PangramChecker.isPangram("My girl wove dozen plaid jackets before she quit"));
Assert.assertFalse(PangramChecker.isPangram("Warm plucky G.H.Q. jinx, to B.V.D.'s"));
}
@Test
public void testPositive() {
// Credits to https://www.thoughtco.com/pangram-word-play-term-1691561
//
Assert.assertTrue(PangramChecker.isPangram("Two driven jocks help fax my big quiz"));
Assert.assertTrue(PangramChecker.isPangram("Pack my box with five dozen liquor jugs"));
Assert.assertTrue(PangramChecker.isPangram("The five boxing wizards jump quickly"));
Assert.assertTrue(PangramChecker.isPangram("Bright vixens jump; dozy fowl quack"));
Assert.assertTrue(PangramChecker.isPangram("Jackdaws love my big sphinx of quartz"));
Assert.assertTrue(PangramChecker.isPangram("John quickly extemporized five tow bags"));
Assert.assertTrue(PangramChecker.isPangram("Waltz, nymph, for quick jigs vex Bud"));
Assert.assertTrue(PangramChecker.isPangram("Quick wafting zephyrs vex bold Jim"));
Assert.assertTrue(PangramChecker.isPangram("Brown jars prevented the mixture from freezing too quickly"));
Assert.assertTrue(PangramChecker.isPangram("Fred specialized in the job of making very quaint wax toys"));
Assert.assertTrue(PangramChecker.isPangram("Sixty zippers were quickly picked from the woven jute bag"));
Assert.assertTrue(PangramChecker.isPangram("We promptly judged antique ivory buckles for the next prize"));
Assert.assertTrue(PangramChecker.isPangram("J.Q. Schwartz flung V.D. Pike my box"));
Assert.assertTrue(PangramChecker.isPangram("Viewing quizzical abstracts mixed up hefty jocks"));
Assert.assertTrue(PangramChecker.isPangram("Farmer jack realized that big yellow quilts were expensive"));
Assert.assertTrue(PangramChecker.isPangram("My girl wove six dozen plaid jackets before she quit"));
Assert.assertTrue(PangramChecker.isPangram("Warm plucky G.H.Q. jinx, fez to B.V.D.'s"));
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment