Data Structures and Performance | Week 2

Course Name: Data Structures and Performance

Course Link: Data Structures and Performance

These are Data Structures and Performance Week 2 Coursera Answers


Programming Assignment: How Easy to Read is Your Writing?

Steps: Create two java files BasicDocument.java and Document.java, create a zip file of both documents and upload in same zip file in both columns

BasicDocument.java:

package document;

import java.util.List;

public class BasicDocument extends Document 
{
    public BasicDocument(String text)
    {
        super(text);
    }
    
    @Override
    public int getNumWords()
    {
        List<String> tokens = getTokens("[a-zA-Z]+");
        return tokens.size();
    }
    
    @Override
    public int getNumSentences()
    {
        List<String> tokens = getTokens("[^!?.]+");
        return tokens.size();
    }
    
    @Override
    public int getNumSyllables()
    {
        List<String> tokens = getTokens("[aeiouyAEIOUY]+");
        List<String> loneEs = getTokens("[^aeiouyAEIOUY]+[eE]\\b");
        List<String> singleEs = getTokens("\\b[^aeiouyAEIOUY]*[eE]\\b");
        
        return tokens.size() - (loneEs.size() - singleEs.size());
    }
    
    public static void main(String[] args)
    {
        testCase(new BasicDocument("This is a test.  How many???  "
                + "Senteeeeeeeeeences are here... there should be 5!  Right?"),
                16, 13, 5);
        testCase(new BasicDocument(""), 0, 0, 0);
        testCase(new BasicDocument("sentence, with, lots, of, commas.!  "
                + "(And some poaren)).  The output is: 7.5."), 15, 11, 4);
        testCase(new BasicDocument("many???  Senteeeeeeeeeences are"), 6, 3, 2);
        testCase(new BasicDocument("Here is a series of test sentences. Your program should "
                + "find 3 sentences, 33 words, and 49 syllables. Not every word will have "
                + "the correct amount of syllables (example, for example), "
                + "but most of them will."), 49, 33, 3);
        testCase(new BasicDocument("Segue"), 2, 1, 1);
        testCase(new BasicDocument("Sentence"), 2, 1, 1);
        testCase(new BasicDocument("Sentences?!"), 3, 1, 1);
        testCase(new BasicDocument("Lorem ipsum dolor sit amet, qui ex choro quodsi moderatius, nam dolores explicari forensibus ad."),
                 32, 15, 1);
    }
}

These are Data Structures and Performance Week 2 Coursera Answers


Document.java:

package document;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Document {

  private String text;
  
  protected Document(String text)
  {
    this.text = text;
  }
  
  protected List<String> getTokens(String pattern)
  {
    ArrayList<String> tokens = new ArrayList<String>();
    Pattern tokSplitter = Pattern.compile(pattern);
    Matcher m = tokSplitter.matcher(text);
    
    while (m.find()) {
      tokens.add(m.group());
    }
    
    return tokens;
  }
  
  protected int countSyllables(String word)
  {
    int count = 0;
    boolean newSyllable = true;
    for (int i = 0; i < word.length(); i++) {
      if(i == word.length()-1 && word.toLowerCase().charAt(i) == 'e' && newSyllable && count>0) {
        count--;
      }
      if(newSyllable && isVowel(word.toLowerCase().charAt(i))) {
        newSyllable = false;
        count++;
      }
      else if(!isVowel(word.toLowerCase().charAt(i))) {
        newSyllable = true;
      }
    }
      return count;
  }
  
  public boolean isVowel(char ch) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
            return true;
        }
        return false;
     }
  
  public static boolean testCase(Document doc, int syllables, int words, int sentences)
  {
    System.out.println("Testing text: ");
    System.out.print(doc.getText() + "\n....");
    boolean passed = true;
    int syllFound = doc.getNumSyllables();
    int wordsFound = doc.getNumWords();
    int sentFound = doc.getNumSentences();
    if (syllFound != syllables) {
      System.out.println("\nIncorrect number of syllables.  Found " + syllFound 
          + ", expected " + syllables);
      passed = false;
    }
    if (wordsFound != words) {
      System.out.println("\nIncorrect number of words.  Found " + wordsFound 
          + ", expected " + words);
      passed = false;
    }
    if (sentFound != sentences) {
      System.out.println("\nIncorrect number of sentences.  Found " + sentFound 
          + ", expected " + sentences);
      passed = false;
    }
    
    if (passed) {
      System.out.println("passed.\n");
    }
    else {
      System.out.println("FAILED.\n");
    }
    return passed;
  }
  
  public abstract int getNumWords();
  
  public abstract int getNumSentences();
  
  public abstract int getNumSyllables();
  
  public String getText()
  {
    return this.text;
  }
  
  public double getFleschScore()
  {
    return 206.835 - (1.015 * (double)getNumWords()/getNumSentences()) 
        - (84.6 * ((double)getNumSyllables())/getNumWords());
  }
  
}

These are Data Structures and Performance Week 2 Coursera Answers

See also  Data Structures and Performance | Week 5

Module and Programming Assignment Quiz

Q1. What will be printed by the following code? Of course, you can run this code and get the question right, but we want you to answer this question (and all of these questions) without running the code.

String s1 = new String("String 1");
String s2 = "String 1";
if (s1 == s2) {
    System.out.println("Equal");
else {
    System.out.println("Not equal");
}
  • Not equal
  • Equal
Answer: Not equal

Q2. Which of the following lines of code correctly assign a String containing the text “My String” to the variable ‘text’?
(Select all correct options.)

Answer:

String s1 = "My String";
String text = s1;
String text = "My ";
String s2 = "String";
text = text + s2;

These are Data Structures and Performance Week 2 Coursera Answers


Q3. What best describes the functionality of the following method?

public int mystery(String s)
{
    char[] letters = s.toCharArray();
    int x = 0;
    for (int i = 0; i < letters.length; i++) {
        if (letters[i] == ' ') {
           letters[i] = '_';
           x++;
        }
    }
    return x;
}
  • It counts the number of spaces in a given string, and returns the count. It does not change the original String.
  • It counts the number of spaces in a given String and returns the count. It also replaces all spaces in the given String with underscore characters (‘_’).
  • It counts the number of total characters in the String and returns the count. It also replaces all spaces in the given String with underscore characters (‘_’).
Answer: It counts the number of spaces in a given string, and returns the count. It does not change the original String.

These are Data Structures and Performance Week 2 Coursera Answers


Q4. Assume you have a String variable s that stores the text
“%one%%two%%%three%%%%”
Which of the following calls to s.split will return the String array:
[“%”, “%%”, “%%%”, “%%%%”]
(Select all correct options.)

See also  Data Structures and Performance | Week 4

Answer:

s.split("[a-z]+");
s.split("one|two|three");

These are Data Structures and Performance Week 2 Coursera Answers


Q5. Assume that you have a Document object stored in the variable d, whose whole text string is
“one (1), two (2), three (3)”
Which of the following calls to getTokens will return the list of Strings given here:
[“one”, “(1)”, “two”, “(2)”, “three”, “(3)”]
Pay attention to the spaces in the original string, and also notice the commas are not preserved in the strings in the list.
(Select all correct options.)

Answer:

Soon


These are Data Structures and Performance Week 2 Coursera Answers


Q6. Does a higher or lower Flesch Index Score indicate simpler text?

  • The higher the Flesch Index Score, the simpler the text and the easier it is to read.
  • The lower the Flesch Index Score, the simpler the text and the easier it is to read.
Answer: The higher the Flesch Index Score, the simpler the text and the easier it is to read.

These are Data Structures and Performance Week 2 Coursera Answers


Q7. How much time did you spend on the programming assignment?

  • <1 hour
  • 1-2 hours
  • 2-3 hours
  • 3-4 hours
  • 4-5 hours
  • 5+ hours
Answer: <1 hour

These are Data Structures and Performance Week 2 Coursera Answers


Q8. How difficult did you find the programming assignment?

  • Very Easy
  • Easy
  • Somewhat Easy
  • Somewhat Difficult
  • Difficult
  • Very Difficult
Answer: Somewhat Easy

These are Data Structures and Performance Week 2 Coursera Answers


Q9. How much did you enjoy the programming assignment?

  • I really enjoyed the programming assignment
  • I enjoyed the programming assignment
  • I’m neutral about the programming assignment
  • I did not enjoy the programming assignment
  • I really did not enjoy the programming assignment
Answer: I enjoyed the programming assignment

These are Data Structures and Performance Week 2 Coursera Answers

See also  Data Structures and Performance | Week 3

More Weeks of the course: Click Here

More Coursera courses: https://progiez.com/coursera

Data Structures and Performance Week 2 Coursera Answers