Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Help! A vb question answer in 30minutes =.=

Author  Topic 

Denil
Starting Member

14 Posts

Posted - 2007-10-26 : 07:08:59
Hello there.. Today i got a mini test ask me to answer this question..too bad i don't know how to answer Can anyone answer this question?

[b]Question:[/b]
Write a Java script/VB script and a servlet on the following:
(i) The JSP should allow the user to enter a list of alphabet and post it to a servlet.
(ii) The servelt will return the number of occurance of each alphabet in a table order by the number of occurance. Please do the sorting of the occurance in descending order

Eg. I fuser enter ABBZZZBZA, the list display should be:
Z B A
4 3 2

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-26 : 07:32:26
What did you try so far?
Using while loop get each character and count it

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Denil
Starting Member

14 Posts

Posted - 2007-10-26 : 10:23:36
Use while loop? how? what is the code?
Did you think array can be use?
Did anyone else can give me some idea or code that work for this question? Thanks you for your help =)
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2007-10-26 : 10:27:15
What particular class is this for? Just curious. Is your professor/teacher cool?
Go to Top of Page

Denil
Starting Member

14 Posts

Posted - 2007-10-26 : 14:51:04
this is a job interview question...That is what my lecturer tell me. It takes from 1 company that testing the fresh graduate student how well they know about programming.
Now i start to scare next time i get those question when interview

Anyone know how to answer this question? how to seperate the element of the alphabet and calculate the amount? tough for me =.=
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-27 : 02:50:03
I don't know any J/VB script, and since this is a MS SQL Server forum, i shall give u a solution in T-SQL

DECLARE @STR varchar(100), @i int

SELECT @STR = 'ABBZZZBZA',
@i = 1

DECLARE @tab TABLE
(
alpha CHAR(1)
)

WHILE @i < LEN(@STR)
BEGIN
INSERT INTO @tab (alpha)
SELECT SUBSTRING(@STR, @i, 1)

SELECT @i = @i + 1
END

SELECT alpha, cnt = COUNT(*)
FROM @tab
GROUP BY alpha



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-10-27 : 11:14:00
>> this is a job interview question

Then, to be honest, you simply do not deserve the job yet. Keep studying and practicing.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-27 : 11:44:24
"i shall give u a solution in T-SQL"

I think you should have used a Tally table
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-29 : 02:22:00
quote:
Originally posted by Kristen

"i shall give u a solution in T-SQL"

I think you should have used a Tally table


I think he followed my front end suggestion

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-29 : 08:11:10
quote:
Originally posted by Kristen

"i shall give u a solution in T-SQL"

I think you should have used a Tally table



That might be too much to comprehend for OP


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-10-29 : 08:25:17
[code]Declare @sent varchar(50)

set @sent = 'ABBZZZBZA'

select letter, count(letter)
from
(
select substring(@sent, t.number, 1) as letter
from master..spt_values as t
where name is null and number between 1 and datalength(@sent)
) t
group by letter
order by 1 desc[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

JBelthoff
Posting Yak Master

173 Posts

Posted - 2007-11-03 : 09:27:00
OK... So it's early and I had nothing else to do. I must say the order by would be much easier in SQL but an Asp.Net approach does work.

I cheated and used MonoSpace font rather than actually creating an HTML table but you'll get the idea.

This is in Asp.Net 2.0 C# and uses a Generic List "List<T>" object and the Sort() method of that object.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<font face="MonoSpace"><%= strOutput %></font>
</div>
</form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected String strInput;
protected String strOutput;
protected Int32 intInputLength;
protected List<DataHolder> l;

protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
// Get Input Value
strInput = txtInput.Text.Trim();

// Get the length of the input
intInputLength = strInput.Length;

// Instantiate out Generic Listt
l = new List<DataHolder>();

// Loop through the input and add DataHolder Classes to the Generic List
for (Int32 i = 0; i < intInputLength; i++)
{
if (GetIndex(strInput.Substring(i, 1)) == -1)
{
DataHolder dh = new DataHolder();
dh.Letter = strInput.Substring(i, 1);
dh.Counter = 1;
l.Add(dh);
}
else
{
l[GetIndex(strInput.Substring(i, 1))].Counter += 1;
}
}

// Sort our List
l.Sort();

// Add a Line Break
strOutput = "<br />";

// Format our first line Output descending on the count.
for (int x = l.Count - 1; x > -1; x--)
{
strOutput += l[x].Letter;
strOutput += " ";
}

// Add another Line Break
strOutput += "<br />";

// Format our second line Output descending on the count.
for (int x = l.Count - 1; x > -1; x--)
{
strOutput += l[x].Counter.ToString();
strOutput += " ";
}
}
}

// Get the index of the list
private Int32 GetIndex(String strLetter)
{
return l.FindIndex(delegate(DataHolder mi_tmp) { return mi_tmp.Letter == strLetter; });
}
}

// Class to hold our data Implementing IComparable
public class DataHolder : IComparable
{
private String _letter;
/// <summary>
/// Holds the Letter
/// </summary>
public String Letter
{
get { return this._letter; }
set { this._letter = value; }
}

private Int32 _counter;
/// <summary>
/// Holds a count of occurances of Letter
/// </summary>
public Int32 Counter
{
get { return this._counter; }
set { this._counter = value; }
}

// Something to compare it to.
public int CompareTo(object obj)
{
DataHolder dh = (DataHolder)obj;
return this.Counter.CompareTo(dh.Counter);
}
}


JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
› As far as myself... I do this for fun!
Go to Top of Page

JBelthoff
Posting Yak Master

173 Posts

Posted - 2007-11-03 : 09:37:52
Just curious,

Did I get the job?

JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
› As far as myself... I do this for fun!
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-11-03 : 14:39:16
quote:
since this is a MS SQL Server forum, i shall give u a solution in T-SQL

Actually this is the ASP.NET forum :-)
However the OP posted to this ASP.NET forum asked for a JSP solution using Javascript or VBScript, so at this stage I'd say this is the most technology confused post ever!!
Go to Top of Page
   

- Advertisement -