Moderators: phlip, Moderators General, Prelates
the_dict["name"] = (cost, value)print the_dict.items() # will print [("name", (cost, value)), ...]def getValue(val):
return val[1][1] # val is (name, (cost, value)) so val[1][1] is value
the_max = max(the_dict.items(), key=getValue)def getValue(val):
return val[1][1] # val is (name, (cost, value)) so val[1][1] is value
the_max = max((i for i in the_dict.items() if i[1][0] <= max_cost), key=getValue)def getValuePerCost(val):
return val[1][1] / val[1][0]
the_max = max(the_dict.items(), key=getValuePerCost)
$catlist = new WP_Query( 'category_name='.$cat->slug.'&post_type=tutorial&orderby=date&order=asc' );
echo '<ul>';
while($catlist->have_posts()){
$catlist->the_post();
$posttitle = the_title();
echo('<li><strong>');
echo($posttitle);
echo('</strong></li>');
}
echo '</ul>';
<ul>
Some Post Name<li><stong></strong></li>
Some Other Post Name<li><stong></strong></li>
</ul>
roband wrote:Mav is a cow.
roband wrote:Mav is a cow.
RoadieRich wrote:... I had some non-working IRC code I could have posted here, but I deleted it in a fit of rage. Is there any way to make py3k use bytestrings by default, or are we forced to subclass socket.socket (which went wrong the first time I tried it) or have 'b"""s everywhere? Why hasn't the distributed socket.socket been updated to take a str?
str = bytes

TheChewanater wrote:RoadieRich wrote:... I had some non-working IRC code I could have posted here, but I deleted it in a fit of rage. Is there any way to make py3k use bytestrings by default, or are we forced to subclass socket.socket (which went wrong the first time I tried it) or have 'b"""s everywhere? Why hasn't the distributed socket.socket been updated to take a str?
Because the Py3k str class only does unicode, which (I assume) cannot be used with sockets. What's wrong with having b''s everywhere?
Although, it's too bad this code doesn't change the default type of quote-enclosed characters. That would be an awesome hack.
- Code: Select all
str = bytes
roband wrote:Mav is a cow.
tasks[task].sp=tasks[task-1].sp - padding;
asm volatile(
"lds r26, %0\n\t"
"ld r28, x+\n\t"
"out __SP_L__, r28\n\t"
"ld r28, x\n\t"
"out __SP_H__, r28\n\t"
: : "x" (&(tasks[task].sp)));
init_function();
It Should Be Real wrote:Fuck the wizard.
We're doing this manually.
Ptolom wrote:If I manually move the stack pointer and then call a function, like this:
- Code: Select all
tasks[task].sp=tasks[task-1].sp - padding;
asm volatile(
"lds r26, %0\n\t"
"ld r28, x+\n\t"
"out __SP_L__, r28\n\t"
"ld r28, x\n\t"
"out __SP_H__, r28\n\t"
: : "x" (&(tasks[task].sp)));
init_function();
Will the function work as normal with its stack where I put it, at least until in comes to return (which will never happen in this case)? I am trying to write a simple multitasking system and the combination of asm and C is baffling me.
tasks[task].sp=tasks[task-1].sp - padding;
call_with_stack(init_function, tasks[task].sp)error: uncaptured python exception, closing channel <__main__.ServConnection connected at 0x5824f0> (<class 'AttributeError'>:'str' object has no attribute 'more' [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asyncore.py|write|89] [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asyncore.py|handle_write_event|455] [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asynchat.py|handle_write|194] [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asynchat.py|initiate_send|245])self.data = self.data.join(self.recv(ac_in_buffer_size))error: uncaptured python exception, closing channel <__main__.ServConnection connected at 0x5819f0> (<class 'TypeError'>:can't concat bytearray to str [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asyncore.py|write|89] [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asyncore.py|handle_write_event|455] [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asynchat.py|handle_write|194] [/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/asynchat.py|initiate_send|257] [bot.py|send|41])
NecklaceOfShadow wrote:I... I kind of hate bytes and strings.
self.data.join(self.recv(ac_in_buffer_size).decode('utf-8'))roband wrote:Mav is a cow.
Eftersom wrote:when I run this method it returns: '[Ljava.lang.String;@1b67f74'
#include <iostream>
#include <string>
using namespace std;
//Define situations
string s0 = "s0";
string s1 = "s1";
string s2 = "s2";
string s11 = "s11";
string s12 = "s12";
string s21 = "s21";
string s22 = "s22";
//Define error codes
string sErrorInputNot1or2 = "Input error. Enter either 1 or 2.\n"; //Error if input is not 1 or 2
//Define variables
string stringinput;
bool dead = false;
int sequence;
int input;
int main ()
{
Restart:
dead = false;
sequence = 0;
input = 0;
while (dead == false) //Loop initializes main code.
{
cout << s0 << "\n"; //cout is "s" plus "sequence"
InputTest:
cin >> input;
if ((input == 1) || (input == 2)) //Error handling
sequence = sequence * 10 + input;
else
{
cout << sErrorInputNot1or2;
goto InputTest;
}
if ((sequence == 222) || (sequence == 211) || (sequence == 111)) //Death handling
{
dead = true;
cout << "Game over!\nEnter 1 to play again.\n";
cin >> input;
if (input == 1)
goto Restart;
}
}
return 0;
}Travuersa wrote:in C++
bool AngryAboutGoto = false;
Lol:
Basically I am just trying to make a text biased adventure that displays string "s0," gets a reply of either 1 or 2, and then displays the next string.
Like this:
s0 //actually a set of words, NOT JUST s0
1
s1 //actually a set of words, NOT JUST s1
2
s12 //actually a set of words, NOT JUST s12
1
s121 //actually a set of words, NOT JUST s121
How would I do this?Spoiler:
#include <shared_ptr> // or is it <memory>? I forget
#include <map>
#include <string>
class State;
typedef std::shared_ptr<State> pState;
struct StateTransition {
std::string description;
pState destination;
// A simple constructor to make initializing this easier:
StateTransition( std::string description_, pState destination_ ):
description(description_), destination(destination_)
{}
};
class State {
public:
virtual StateTransition Execute() = 0;
virtual ~State() {}
State() {};
};
class SimpleState;
typedef std::shared_ptr<SimpleState> pSimpleState;
class SimpleState: public State {
private:
std::map< std::string, StateTransition > exitMap;
std::string description;
protected:
virtual std::string OnBadInput( std::string command )
{
return "I don't understand. Please try again.\n";
}
public:
virtual StateTransition Execute(); // implement below
void AddExit( std::string command, StateTransition transition )
{
exitMap[command] = transition;
}
void SetDescription( std::string description_ )
{
description = description_;
}
SimpleState() {}
};
class TrivialStateEngine {
public:
void RunEngine( pState start_state );
};
// in .cpp:
StateTransition SimpleState::Execute()
{
std::cout << description;
// repeat until we return:
while(true) {
std::string user_command;
std::getline(std::cin, user_command);
if (exitMap.find(user_command) == exitMap.end()) {
std::cout << OnBadInput( user_command );
continue; // try again
};
return exitMap[ user_command ];
};
}
void TrivialStateEngine::RunEngine( pState start_state )
{
pState current_state = start_state;
while (current_state)
{
StateTransition transition = current_state->Execute();
std::cout << transition.description;
current_state = transition.destination;
};
}
int main()
{
pSimpleState s1(new SimpleState);
s1->SetDescription("Room s1");
pSimpleState s2(new SimpleState);
s2->SetDescription("Room s2");
pSimpleState s11(new SimpleState);
s11->SetDescription("Room s11");
pSimpleState s12(new SimpleState);
s12->SetDescription("Room s12");
pSimpleState s21(new SimpleState);
s21->SetDescription("Room s21");
pSimpleState s22(new SimpleState);
s22->SetDescription("Room s22");
s1->AddExit( "1", StateTransition( "Going to room s11", s11 );
s1->AddExit( "2", StateTransition( "Going to room s12", s12 );
s2->AddExit( "1", StateTransition( "Going to room s21", s21 );
s2->AddExit( "2", StateTransition( "Going to room s22", s22 );
s11->AddExit( "e", StateTransition( "Exiting game", pState() );
s12->AddExit( "e", StateTransition( "Exiting game", pState() );
s21->AddExit( "e", StateTransition( "Exiting game", pState() );
s22->AddExit( "e", StateTransition( "Exiting game", pState() );
TrivialStateEngine engine;
engine.RunEngine( s1 );
return 0;
}
poxic wrote:You, sir, have heroic hair.
e^iπ+1=0 wrote:Er... this is the coding forum. Accordingly, this is the thread for code that doesn't work.
int main(){
for (int a = 1, bool finished = false; not finished; a++){
if (a == 4) finished = true;
}
}
g++ wrote:In function `int main()':
2: error: expected unqualified-id before "bool"
2: error: expected `,' or `;' before "bool"
2: error: `finished' undeclared (first use this function)
2: error: (Each undeclared identifier is reported only once for each function it appears in.)
userxp wrote:I'm a noob in C++, but isn't
- Code: Select all
int main(){
for (int a = 1, bool finished = false; not finished; a++){
if (a == 4) finished = true;
}
}
supposed to work?g++ wrote:In function `int main()':
2: error: expected unqualified-id before "bool"
2: error: expected `,' or `;' before "bool"
2: error: `finished' undeclared (first use this function)
2: error: (Each undeclared identifier is reported only once for each function it appears in.)
THERE IS A COMMA!
int a = (1, bool finished = false)Yakk wrote:I'm guessing it is parsing that as:
- Code: Select all
int a = (1, bool finished = false)
, is also an operator, annoyingly enough. And that can cause weird parsing quirks. Possibly this is one of them.
As an aside, start using ++a instead of a++. The post-increment operator is a strange beast, and you should use it only when you need to use it (even if, with integers, in this case, they do the same thing).
Yakk wrote:Yes, the performance hit from overloaded post-increment is one reason to use pre-increment. The other reason is the ridiculous ease of invoking undefined behavior without realizing it if you use post-increment in non-trivial situations. Relatively few C/C++ programmers understand sequence points fully, and without understanding sequence points fully, non-trivial use of post-increment is not recommended.
Post increment is a strange beast.
userxp wrote:I'm a noob in C++, but isn't
- Code: Select all
int main(){
for (int a = 1, bool finished = false; not finished; a++){
if (a == 4) finished = true;
}
}
supposed to work?
int main ( ) {
for (int a = 1; a <= 4; a++) {
// ...
}
}int main ( ) {
for (int a = 1; true; a++) {
// ...
if (a == 4) break;
// ...
}
}EvanED wrote:Hmm, not to threadjack, but I'm having a problem thinking of when a statement has defined behavior with pre-increment but not if you replace it with post-increment. Care to elaborate a little?
i = i++i = ++iYakk wrote:The simplest example is:
- Code: Select all
i = i++
There is no sequence point above, so when the increment occurs is anytime after the RHS's value is evaluated. In particular, it could occur either before or after the RHS is assigned to the LHS. So the above could either NOOP, or increment i.
this has fully defined behavior.
- Code: Select all
i = ++i
Yakk wrote:this has fully defined behavior.
- Code: Select all
i = ++i
"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."
import random
print(
"""
-The Number guesser-
Think of a number between 1 and 100
and I will try to guess it
mode 1 is 1-10, mode 2 is 1-100
reply "yes" or "no"
"""
)
tries=0
mode=input("Mode 1 or 2? ")
if mode==1:
response=""
while response!="yes":
guess=random.randint(1,10)
print("\nMy guess is", guess)
response=input("\nAm I correct?")
if response=="no":
tries+=1
elif mode==2:
response=""
while response!="yes":
guess=random.randint(1,100)
print("\nMy guess is", guess)
response=input("\nAm I correct?")
if response=="no":
tries+=1
print("\nIt took me", tries, "tries")
input("\n\nPress enter to exit")Users browsing this forum: Breakfast, GuetraGma, MobTeeseboose and 6 guests