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...

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

  • 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....

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

  • Write a C Program that uses Command Line Arguments to convert the input string at the...

    Write a C Program that uses Command Line Arguments to convert the input string at the Command Line into Upper-Case, by traversing the argument at Command Line with the help of pointers.

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • 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

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