題目:
我的程式:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBoxOutput.Text = ""
Dim InputMsg As String = TextBoxInput.Text
Dim EnglishWords As Integer = 26
Dim Character As String() = New String(EnglishWords - 1) {} '(0 ~ [EnglishWords]-1)
Dim EnglishWordsArray As Integer() = New Integer(EnglishWords - 1) {}
For IndeedWords = 1 To Len(InputMsg)
For j As Integer = 0 To EnglishWords - 1
If LCase(Mid(InputMsg, IndeedWords, 1)) = Chr(j + 97) Then
Character(j) += 1
End If
Next
Next
Dim a = 97 '10進位ASCII
Dim z = 122
For o = 0 To EnglishWords - 1
For i = a To z
EnglishWordsArray(o) = i
Next
Next
Array.Sort(Character, EnglishWordsArray)
Array.Reverse(Character)
Array.Reverse(EnglishWordsArray)
For Output = 0 To UBound(Character)
If Character(Output) <> 0 Then
TextBoxOutput.Text &= Chr(EnglishWordsArray(Output)) & "=" & Character(Output) & ";"
End If
Next
End Sub
End Class
我的結果:
原本以為能正常顯示對應英文和次數
結果無法
可以的話告訴我錯的位置以及原因
Public Class Form1
Private Sub Sort(sender As Object, e As EventArgs) Handles ButtonSort.Click
TextBoxOutput.Text = ""
Dim InputMsg As String = TextBoxInput.Text
Dim EnglishWords As Integer = 26
Dim Character As String() = New String(EnglishWords - 1) {}
Dim EnglishWordsArray As Integer() = New Integer(EnglishWords - 1) {}
For IndeedWords = 1 To Len(InputMsg)
For j As Integer = 0 To EnglishWords - 1
If LCase(Mid(InputMsg, IndeedWords, 1)) = Chr(j + 97) Then
Character(j) += 1
End If
Next
Next
Dim i = 97
For o = 0 To EnglishWords - 1 Step 1
EnglishWordsArray(o) = i
i += 1
Next
Array.Sort(Character, EnglishWordsArray)
Array.Reverse(Character)
Array.Reverse(EnglishWordsArray)
For Output = 0 To UBound(Character)
If Character(Output) <> 0 Then
TextBoxOutput.Text &= Chr(EnglishWordsArray(Output)) & "=" & Character(Output) & "; "
End If
Next
End Sub
End Class
能正常運作,但還不是題目要求的所有字元
接下來的疑問是
大寫部分和小寫如何並存以及空格部分
空格 ASCII: 32
大寫A~Z ASCII: 65 ~ 90
小寫a~z ASCII: 67 ~ 122
我是在想直接包32~122
但這樣運算元好像有點多
雖然暴力破解好像不錯用
Public Class Form1
Private Sub Sort(sender As Object, e As EventArgs) Handles ButtonSort.Click
TextBoxOutput.Text = ""
Dim InputMsg As String = TextBoxInput.Text
Dim CharacterCalculation As Integer = 90
Dim Character As String() = New String(CharacterCalculation - 1) {}
Dim EnglishWordsArray As Integer() = New Integer(CharacterCalculation - 1) {}
Dim StartWords As Integer = 32
For IndeedWords = 1 To Len(InputMsg)
For j As Integer = 0 To CharacterCalculation - 1
If Mid(InputMsg, IndeedWords, 1) = Chr(j + StartWords) Then
Character(j) += 1
End If
Next
Next
Dim i = StartWords
For o = 0 To CharacterCalculation - 1
EnglishWordsArray(o) = i
i += 1
Next
Array.Sort(Character, EnglishWordsArray)
Array.Reverse(Character)
Array.Reverse(EnglishWordsArray)
For Output = 0 To UBound(Character)
If Character(Output) <> 0 Then
TextBoxOutput.Text &= Chr(34) & Chr(EnglishWordsArray(Output)) & Chr(34) & "=" & Character(Output) & ";" & " "
End If
Next
End Sub
End Class
For i = a To z
EnglishWordsArray(o) = i
Next
你這段的寫法
在迴圈跑完之後
EnglishWordsArray(o) 都會是「最後一個 i」
也就是z
你要的可能是
For o = 0 To EnglishWords - 1
EnglishWordsArray(o) = o + 97
Next
https://ideone.com/EFmvXF
python3
testStr = "gdsfgfdhjghfgsdf"
def sortBy(item):
return item[1]
result = sorted([[str(c), testStr.count(c)] for c in set(testStr)], key=sortBy, reverse=True)
print(";".join('"{}"={}'.format(i[0], i[1]) for i in result))
所以說你這個比賽真的沒限制語言嗎?XD
js 參戰
const encoding = (input) => {
return [...new Set(input)].map((el) => {
const temp = {};
const regexp = new RegExp(`[^${el}]`, 'g');
temp[el] = input.replace(regexp, '').length;
return temp;
}).sort((a, b) => {
return Object.values(a) > Object.values(b)
? -1
: Object.values(a) < Object.values(b)
? 1
: Object.keys(a) > Object.keys(b)
? 1
: -1;
}).map(el2 => `"${Object.keys(el2)[0]}"=${Object.values(el2)[0]}`)
.join(';');
}
const result = encoding('Conventional approaches for encoding technique');
console.log(result);
是說他這題目根本有問題吧
輸出我找不出來有任何規則
原本以為是
sort by key 然後 sort by value
但看起來不是
或者是 sort by value 然後 value 一樣的 照原字串順序排
但是看到 c 跟 a 的輸出位置就知道不是
難道是要自己寫 sort 規則!?
而且後半部的; 間空格不一樣是哪招 XD
回正題
你的輸出 value 看起來沒問題的
但是 key 不對
所以你可能要看一下你產生 key 那部份的 code
C# 參戰
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestArea
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please Input Your String:");
string userInput = Console.ReadLine();
Dictionary<char, int> dictionary = new Dictionary<char, int>();
List<char> characterList = userInput.ToList();
foreach (char character in characterList)
{
if (dictionary.Keys.Contains(character))
{
dictionary[character] += 1;
}
else
{
dictionary.Add(character, 1);
}
}
List<KeyValuePair<char, int>> keyValuePairList = dictionary.ToList();
keyValuePairList = keyValuePairList.OrderByDescending(pair => pair.Value).ToList();
foreach (KeyValuePair<char, int> pair in keyValuePairList)
{
Console.Write("\"{0}\":{1}; ", pair.Key, pair.Value);
}
Console.ReadKey();
}
}
}
php 參戰
$str = 'Conventional approaches for encoding technique';
$result = [];
$arr = [];
$items = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($items as $item){
isset($result[$item]) ? $result[$item]++ : $result[$item] = 1;
}
arsort($result);
foreach ($result as $key => $item){
//方法一 echo $key . '=' . $item . ';';
$arr[] = $key . '=' . $item;
}
echo implode(';', $arr);