Question

Write a program that determines if a string (passed as a command-line argument) has all unique...

Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique.

Special requirements:
You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings).
You may have only one string (the one received as the command-line argument).
You may have one boolean variable and up to 3 integer variables (hint: one is the counter).


DO NOT use any built-in String methods, such as the ones listed here:
https://www.w3schools.com/python/python_ref_string.asp

Input validation: You must print a friendly error message and end execution if the program does not receive any arguments from the command-line.

Here are some sample runs:

C:\>python unique.py
Usage: python unique.py "Any string you want..."
C:\>python unique.py This is a string!
Usage: python unique.py "Any string you want..."
C:\>python unique.py "This is a string!"

T : 1
h : 1
i : 3
s : 3
: 3
i : 3
s : 3
: 3
a : 1
: 3
s : 3
t : 1
r : 1
i : 3
n : 1
g : 1
! : 1

Not unique! One or more characters appear more than once.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import sys # for command line argumets

# length on command line argument 1 then no arguments are provided
if len(sys.argv) == 1:
    # print error
    print("Error: Plase Enter Something After File Name in Command Line!")
else:
    # lets assume all charachtesr are unique in the string
    is_unique = True
    # a loop for iterating over each character
    for i in range(len(sys.argv[1])):
        # count of each char is 0
        counter = 0
        # iterate over the string for each character
        for j in range(len(sys.argv[1])):
            # check if there are any matches
            if sys.argv[1][i] == sys.argv[1][j]:
                # if match then increase the counter
                counter += 1
            # check if count for any character is more than one
            if counter > 1:
                #set is_unique to false
                is_unique = False
        #for each character output the character and its count
        print("{} : {}".format(sys.argv[1][i],counter))
    # check the value of is_unique
    if is_unique:
        # print unique
        print("Unique! all characters only appear once.")
    else:
        # print not unique
        print("Not unique! One or more characters appear more than once.")

# used one boolean and 3 integers variables

# if you have any query regardin this please comment and if you like the answer please upvote :)

Add a comment
Know the answer?
Add Answer to:
Write a program that determines if a string (passed as a command-line argument) has all unique...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Write a C program that parses a string (command line) and tokenize it by breaking the...

    Write a C program that parses a string (command line) and tokenize it by breaking the string characters into words that are separated by delimiters that can be white spaces (space and/or tab characters). It also must report how many commands in the input. This program will be reused for your next C programming assignment that is a simple shell interpreter/program called "tech shell". % a.out please enter a string: this is a test 1: this 2: is 3: a...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Help please Write a program named one.c that takes a single command-line argument, the name of...

    Help please Write a program named one.c that takes a single command-line argument, the name of a file. Your program should read all the strings (tokens) from this file and write all the strings that are potentially legal words (the string contains only upper-case and lower-case characters in any combination) to the file words. Your program should ignore everything else (do not write those strings anywhere 1. As an example, running /a.out dsia would result in the generation of the...

  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

  • Create a program that determines and displays the number of unique characters in a string entered...

    Create a program that determines and displays the number of unique characters in a string entered by the user. For example, Hello NITS Students! has 15 unique characters, while zzzz has only one unique character. python only with instructions

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • This program will require you to create a basic parser for an input string that is...

    This program will require you to create a basic parser for an input string that is somewhat like Python. Python has an older parser module and a newer ast module to manage pure Python syntax, but they won't be useful here. The program will read a data string, identify the control characters, and print out the overall structure of the string. Here's an example Input String: [{1}] Output: Number inside a dictionary inside a list Here are some basic rules...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusi...

    Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string (‘A’<->’Z’, ‘B’<->’Y’, ‘C’<->’X’, etc). You should divide this conversion task among the n threads as evenly as possible. Print out the string both before...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT