Recent Entries 10
- pattern minor 112d agoPig latin translating programAbout 2 months ago I started studying python and then 3 days ago sat and began to write a program (found the dumbest & simplest thought I could: a pyglatin translator). I finally got it working 100%. Being a beginner I would love to hear some constructive criticism on my program; I know I have probably written more then was needed on some stuff and taken a long way around on others. ``` another_word = " " sentence1 = " " new_list = [] another = " " def trans_one_word (i): global another_word pyg = 'ay' original = (i) if len(original) > 0 and original.isalpha(): word = original.lower() first = word[0] new_word = word + first + pyg another_word = new_word[1:len(new_word)] return another_word else: print('empty') def input_state(): sentence = input("enter a word to be translated: ") list = sentence.split() for i in list: trans_one_word(i) new_list.append(another_word) s = " " print (s.join( new_list )) input_state() another = input("Would you like to enter another sentence to be translated? \n Y/N") if (another == 'y') or (another == 'Y'): input_state() ``` All in all I am proud of myself for actually writing it without any help, or all of it except the last "if" statement (I had minor problems with it).
- pattern minor 112d agoConverting regular text phrase to Pig Latin in GroovyAm I using the best conventions or are there better ways? ``` static void makePigLatin(String text){ String[] textArray = text.split(" ") String buffer, translated for (int i = 0; i vowels.any {a.contains(it)}}) { translated = textArray[i].substring(j) break } else { buffer = "$buffer${textArray[i][j]}" } } if (textArray[i][textArray[i].length() - 1].find { a -> punctuation.any {a.contains(it)}}) { print "${translated.substring(0, translated.length() - 2)}$buffer" + "ay" + "${textArray[i][textArray[i].length() - 1]}" + " " } else { print "$translated$buffer" + "ay " } } println "" } ``` The vowels and punctuation variables are global: ``` static def vowels = ["a", "e", "i", "o", "u", "y"] static def punctuation = [",", ".", "?", "!", ":"] ```
- pattern minor 112d agoHaskell - Pig Latin TranslatorWhat improvements can I make to the following Pig Latin translator? So far, I've been wondering about the `FlexibleInstances` declaration, but I can't think of how to remove it (since it's required for `PigLatin String` in `instance`s, which is required for me to use `" "` in the `Monoid` instance; I need the extra type argument for `Functor`). ``` {-# LANGUAGE FlexibleInstances #-} import Control.Monad(when) import Data.Char (toLower, toUpper) import System.IO (hFlush, stdout) data PigLatin a = PigLatin a instance Monoid (PigLatin String) where mempty = PigLatin "" mappend (PigLatin a) (PigLatin b) = PigLatin $ a ++ " " ++ b instance Functor PigLatin where fmap f (PigLatin x) = PigLatin $ f x instance Show (PigLatin String) where show (PigLatin s) = s data English a = English a deriving (Eq) safeTail :: [a] -> [a] safeTail (_:xs) = xs safeTail [] = [] quitCommand :: English String quitCommand = English "\\q" wordToPig :: English String -> PigLatin String wordToPig (English word@(x:_)) = PigLatin $ if isVowel x then word ++ "yay" else rearrangedWord ++ "ay" where rearrangedWord = drop (length initialConsonants) word ++ initialConsonants initialConsonants = takeWhile isConsonant word isConsonant = not . isVowel wordToPig (English "") = PigLatin "" isVowel :: Char -> Bool isVowel letter = letter `elem` ['a', 'e', 'i', 'o', 'u'] englishWords :: English String -> [English String] englishWords (English input) = map English $ words input sentenceToPig :: English String -> PigLatin String sentenceToPig = fmap (capitalize . safeTail . map toLower) . foldl mappend mempty . map wordToPig . englishWords capitalize :: String -> String capitalize (x:xs) = toUpper x : map toLower xs capitalize [] = "" prompt :: String -> IO (English String) prompt promptInput = do putStr promptInput hFlush stdout input " when (input /= quitCommand
- pattern major 112d agoIt'th wabbit theathonI don't know regex. I want to learn regex, but learning regex just for the sake of regex would be quite boring. So I decided to learn Ruby as well (which so far looks a lot like Python, which I'm familiar with). My first program attempts to speak with a combination of both Daffy Duck's lisp and Elmer Fudd's verbal apraxia. It works quite well, but it looks quite messy already. How does one properly combine Ruby and (repeated) regex substitutions? ``` def lisp(input) return input.downcase.gsub(/s/, "th").gsub(/er/, "uh").gsub(/r|l/, "w").capitalize end print lisp("What's your input?") user_input = gets.chomp puts lisp(user_input) ``` I downcase everything to make the regex easier and capitalize at the end to turn it back into a proper sentence. Usage: `What'th youw input? It's rabbit season! ` It'th wabbit theathon! `What'th youw input? My super soaker carries 2 liters of water! ` My thupuh thoakuh cawwieth 2 wituhth of watuh!
- pattern minor 112d agoPig Latin Translator in JavaFor starters, I'm a student taking AP Computer Science this year, loving it so far. Lots of fun, especially problems like this. I think I'm going to make it a regular practice to post some of my exercises and projects here for people to review, so I can get some feedback from programmers other than my teacher on different ways to do things, ways to clean up my code, and good practice. I'm not looking for answers here. I've completely written the code and am just looking for some feedback on a few questions listed below. The prompt is as follows: Write an interactive program that reads lines of input from the user and converts each line into Pig Latin. Terminate the program when the user types a blank line. - Words beginning with consonants have the consonant moved to the end of the word and "ay" appended - Words beginning with vowels simply have "ay" appended Specific questions: - Did I do this in an acceptably efficient way? What other, possibly better, ways could I have accomplished this? - How could this be done without an `ArrayList`? Learning how to use `ArrayList`s is something I've done on my own time, not that we've learned in class, and I want to become more independent from them on problems like this in case my teacher doesn't want me to keep using them. - What variables do I need to keep as class variables? When I was declaring them I wasn't sure exactly how I would structure the program, or if I was going to organize it into separate methods or not. ``` import java.util.*; public class PigLatin { static ArrayList al = new ArrayList(); static Scanner sc = new Scanner(System.in); static String userString; static String latinString; static String temp; public static void main(String[] args) { while (true) { latinString = ""; System.out.print("Enter a string to be converted into Pig Latin. To stop, enter a blank input: "); String userString = sc.nextLine();
- pattern moderate 112d agoC++ Pig Latin programI'm just wanting some input on to how others would approach this problem. I am learning and wish to gain insight on others techniques. Be as critical as you need. I want to learn. I feel this code is sloppy and the logic is confusing. ``` /* Pig Latin Write a program that reads a sentence as input and converts each word to “Pig Latin.” In one version, to convert a word to Pig-Latin you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHT Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY */ #include #include using namespace std; // takes a string argument and returns the pigLatin equivalent string pigLatin(string); int main() { string mySentence; getline(cin, mySentence); mySentence = pigLatin(mySentence); cout << mySentence << endl; return 0; } string pigLatin(string word){ //pigLatWord holds word translated in pig latin. //pigLatSentence holds entire translated sentence. string pigLatWord, pigLatSentence = ""; int length = 0, index = 0; while (word[index] != '\0'){ // .find returns -1 if no match is found if (word.find(' ', index) != -1){ length = word.find(' ', index); length -= index;//length - index = the number of characters in a word pigLatWord = word.substr(index, length); pigLatWord.insert(length, "ay"); pigLatWord.insert(length, 1, word[index]);//first letter is inserted at the end of the string pigLatWord.erase(0, 1);// erase first letter in string index += length + 1;//adding one moves index from 'space' to first letter in the next word } else{ pigLatWord = word.substr(index); length = pigLatWord.length(); pigLatWord.insert(length, "ay"); pigLatWord.insert(length, 1, word[index]);
- pattern minor 112d agoWord Separator and Pig Latin Program Final EditThis is an updated version of my previous Java program that asks the user for a sentence and then separates the words and then converts the sentence into Pig Latin. My previous program was working, but there was some tweeking that needed to be done which I did after getting positive feedback. Apparently, my loop in the separateWords function was checking for capital letters twice or so. I have included the instructions for the assignment for reference and the code for peer edit. Thank you. - The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the roses" - Then the program will convert each word in the result string of task 1 into "Pig Latin". In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word. For example, for the result string "Stop and smell the roses" in task 1, the Pig Latin string should be "topSay ndaay mellsay hetay osesray" Requirements: - Your program should have 3 methods: a main method, a method for task 1, and a method for task2 - the method for task 1 should return the result string of task1 - the method for task 2 should display the Pig Latin string ``` public class Assignment9 { public static void main(String[] args) { // Variables String sentence, revisedSentence; // Create a Scanner object for keyboard input Scanner keyboard = new Scanner(System.in); // Get the input string System.out.print("Enter sentence: "); sentence = keyboard.nextLine(); //Close keyboard keyboard.close(); // Call function to perform Task 1 revisedSentence = separateWords(sentence); System.out.pr
- snippet minor 112d agoSeparate combined words in a sentence and convert them into Pig LatinMy program is complete and working, but I would like second opinions on it. Here are the instructions for the assignment: - The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRoses" would be converted to "Stop and smell the roses". - Then the program will convert each word in the result string of task 1 into "Pig Latin". In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word. For example, for the result string "Stop and smell the roses" in task 1, the Pig Latin string should be "topSay ndaay mellsay hetay osesray". Requirements: - Your program should have 3 methods: a main method, a method for task 1, and a method for task2. - The method for task 1 should return the result string of task1. - The method for task 2 should display the Pig Latin string. ``` import java.util.Scanner; public class Assignment9 { public static void main(String[] args) { // Variables String sentence, revisedSentence, latin = " "; // Create a Scanner object for keyboard input Scanner keyboard = new Scanner(System.in); // Get the input string System.out.print("Enter sentence: "); sentence = keyboard.nextLine(); //Close keyboard keyboard.close(); // Task 1 revisedSentence = WordSeparator(sentence); System.out.print("Revised Sentence: " +revisedSentence); // Task 2 PigLatin(revisedSentence, latin); System.exit(0); } private static String WordSeparator(String sentence) { // Variables StringBuilder str = new StringBuilder(sentence); int i = 1; // While loop repeats until th
- pattern moderate 112d agoPig Latin in JavaI've done my second application in Java now. I want to know if: - This code is efficient - This code can be written in a shorter and faster way - Any flaws - Any misuse or better use of concepts Main Class: ``` package Excercises; public class Main { public static void main(String[] args) { PigLatin pigLatin = new PigLatin(); pigLatin.initializeText(); pigLatin.getWord(); pigLatin.convertWordToStringArray(); pigLatin.convertArrayToList(); pigLatin.moveFirstLetterToLast(); pigLatin.mergeList(); System.out.println(pigLatin.returnNewWord()); } } ``` PigLatin class: ``` package Excercises; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PigLatin { private String word; private String newWord; private String[] Array; private List list = new ArrayList(); public void initializeText() { System.out.print("Welcome to the Pig Latin Game! Please enter a word: "); } public void getWord() { Scanner scan = new Scanner(System.in); this.word = scan.next(); } public void convertWordToStringArray() { this.Array = word.split(""); } public void convertArrayToList() { for(String s : this.Array) { this.list.add(s); } } public void moveFirstLetterToLast() { this.list.add("-"); this.list.add(this.Array[0]); this.list.remove(0); this.list.add("ay"); } public void mergeList() { for(int i = 0; i < list.size(); i++) { this.newWord = this.newWord + list.get(i); } this.newWord = this.newWord.replaceAll("null", ""); } public String returnNewWord() { return this.newWord; } } ```
- pattern minor 112d agoConverting the first and last name to pig latinThe following question was taken from Absolute Java 5th ed. by Walter Savitch: Write a program that starts with the string variable first set to your first name and the string variable last set to your last name. Both names should be all lower- case. Your program should then create a new string that contains your full name in pig latin with the first letter capitalized for the first and last name. Use only the pig latin rule of moving the first letter to the end of the word and adding “ay.” Output the pig latin name to the screen. Use the substring and toUpperCase methods to construct the new name. For example, given first = "walt"; last = "savitch"; the program should create a new string with the text “Altway Avitchsay” and print it. This is the code that I have written: ``` public class Question3 { private static String first; private static String last; private static String newName; public static void main(String[] args) { Question3 name = new Question3("walt", "savitch"); System.out.println(name.convertName(first) + " " + name.convertName(last)); } public Question3(String lowerCaseFirstName, String lowerCaseLastName) { first = lowerCaseFirstName; last = lowerCaseLastName; } private String convertName(String originalName) { String firstLetter = originalName.substring(0, 1); newName = nameWithoutFirstLetter(originalName) + firstLetter; // move first letter to the end newName = capitalizeFirstLetter() + nameWithoutFirstLetter(newName) // capitalize first letter and add "ay" to the end + "ay"; return newName; } private String capitalizeFirstLetter() { String capitalFirstLetter = newName.substring(0, 1).toUpperCase(); return capitalFirstLetter; } private String nameWithoutFirstLetter(String name) { String restOfName = name.substring(1); return restOfName;