problem set. please help

PROBLEM #3
As we know, in an n-based number system, there are n different types of digits. In this way, a 1-based number system has only 1 type of digit, the ‘0’. Here are the rules to interpret 1-based numbers. Each number consists of some space separated blocks of 0. A block may have 1, 2 or more 0s. There is a ‘flag’ variable associated with each number

 A block with a single 0 sets ‘flag’ variable to 1

 A block with two 0s sets the ‘flag’ to 0

 If there are n (n > 2) 0s in a block, n-2 binary digits with the current value of flag is appended to your number.
Note that, the first block of every number will have at most 2 0s. For example, the 1-base number 0 0000 00 000 0 0000 is equivalent to binary 11011.

 1st block sets the flag to 1.

 2nd block has 4 0s. So append flag (= 1) 42 = 2 times (11).

 3rd block has 2 0s. Set the flag to 0

 4th block has 3 0s. Append flag (= 0) 3 − 2 = 1 time (110).

 5th block has a single 0. Set flag = 1

 6th and block has 4 0s. Append flag (= 0) 4 − 2 = 2 times (11011).

The final binary number won’t have more than 30 digits. Once, you’ve completed the process, convert the binary value to decimal & print, you’re done!

Example

Enter n-based number: 0 0000 00 000 0 0000

Decimal equivalence: 27

Enter n-based number: 0 000

Decimal equivalence: 1

PROBLEM SET #4
Many internet protocols these days include the option of associating a media type with the content being sent. The type is usually inferred from the file extension. You are to write a program that facilitates the lookup of media types for a number of files.
You will be given a table of media type associations that associate a certain file extension with a certain media type. You will then be given a number of file names, and tasked to determine the correct media type for each file. A file extension is defined as the part of the file name after the final period. If a file name has no periods, then it has no extension and the media type cannot be determined. If the file extension is not present in the table, then the media type cannot be determined. In such cases you will print “unknown” as the media type. If the file extension does appear in the table (case matters), then print the associated media type.

Input: Input begins with 2 integers N and Q on a line. N is the number of media type associations, and Q is the number of file names. Following this are N lines, each containing a file extension and a media type, separated by a space. Finally, Q lines, each are containing the name of a file.
N and Q will be no greater than 100 each. File extensions will consist only of alphanumeric characters, will have length at most 10, and will be distinct. Media types will have length at most 50, and will contain only alphanumeric characters and punctuation. File names will consist only of alphanumeric characters and periods and have length at most 50.

Output: For each of the Q file names, print on a line the media type of the file. If there is no matching entry, print “unknown” (quotes for clarity).

Example

Enter Number of Media Type Association (N): 5

Enter Number of File Names (Q): 6

Media Type 1: html text/html

Media Type 2: htm text/html

Media Type 3: png image/png

Media Type 4: svg image/svg+xm

Media Type 5: txt text/plain

File Name 1: index.html

File Name 2: this.file.has.lots.of.dots.txt

File Name 3: nodotsatall

File Name 4: virus.exe

File Name 5: dont.let.the.png.fool.you

File Name 6: case.matters.TXT

File 1 is text/html

File 2 is text/plain

File 3 is unknown

File 4 is unknown

File 5 is unknown

File 6 is unknown

create a hashmap for < File Extension , Media Type > -->< String, String >

then for each of q queries

get input.substring(lastIndexOf(’.’),input.length())

if the hashmap contains the key print the .get() else print unknown.

i dont get it. pleaSE make the whole code… thanks

import java.io.;
import java.util.
;
public class Main
{
public static void main(String []args)throws IOException{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number of Media Type Association (N): ");
int n=Integer.parseInt(x.readLine());
System.out.println("Enter Number of File Names (Q): ");
int q=Integer.parseInt(x.readLine());
HashMap<String,String> a=new HashMap<String,String>();
for(int i=1;i<=n;i++){
System.out.println("Media Type “+i+”: “);
String parts[]=x.readLine().split(” ");
a.put(parts[0],parts[1]);
}
for(int i=1;i<=q;i++){

            System.out.println("File Name "+i+": ");
            String w=x.readLine();
            String h=w.substring(w.lastIndexOf('.')+1,w.length());
            if(a.containsKey(h)) System.out.println(a.get(h));
            else System.out.println("unknown");
        }
    }
}
1 Like

i hope this is what you need

code is not working…

It works, unless you don’t know how to run that on your local machine O.o

Solution to PROBLEM #3

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	private static String getBinary(String base0)
	{
		StringTokenizer st = new StringTokenizer(base0, " ");
		char flag = '0';
		String ans = "";
		while (st.hasMoreTokens())
		{
			String term = st.nextToken();
			if (term.length() == 1)
				flag = '1';
			else if (term.length() == 2)
				flag = '0';
			else
				for (int i = 2; i < term.length(); ++i)
					ans = ans + flag;
		}
		return ans;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = br.readLine();
		s = getBinary(s);
		int n = Integer.parseInt(s, 2);
		System.out.println("Decimal equivalent: " + n);
	}
}

You can also find this at http://ideone.com/KWpEu3